Last year, for some reason, I needed to output the binary form of a file to a text file in text format, similar to the following:
4d 5A 90 00 03 00 00 00 04 00 00 ff 00 00 00
B8 00 00 00 00 00 00 40 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 D0 00 00 00
0e 1f Ba 0e 00 B4 09 CD 21 B8 01 4C CD 21 54 68
69 73 20 70 72 6f 67 72 61 6D 20 63 61 6e 6e 6f
74 20 62 65 20 72 75 6e 20 69 6e 20 44 4f 53 20
6D 6f 64 65 2E 0d 0d 0a 24 00 00 00 00 00 00
......
What I think is simple: open the file, read the file, use a loop, use wsprintf for each byte, connect it with lstrcat, and write the file. As a result, I can easily see the following sections without syntax errors:Code:
// Note 1: You can understand some of the undefined variables as global variables.
// NOTE 2: New is a macro function defined by me. It is modeled like operator new of C ++.
// # Define new (type, count) (type *) (malloc (sizeof (type) * (count )))
Void save (void)
{
DWORD dwsize, dwreaded, I;
Tchar szbyte [5];
// Read the source file
Hfilesrc = createfile (szfilesrc, generic_read, 0, null, open_always, 0, null );
Dwsize = getfilesize (hfilesrc, null );
Lpbysrc = new (byte, dwsize );
Readfile (hfilesrc, (lpvoid) lpbysrc, dwsize, & dwreaded, null );
// The following mysize is a macro that indicates the buffer size. Because the calculation size is cumbersome and irrelevant to this article, I will skip it here
Lpdst = new (tchar, mysize );
* Lpdst = '\ 0 ';
For (I = 0; I <dwsize-1; I ++)
{
If (I % 16 = 15) // process line feed
Wsprintf (szbyte, "% 02x \ r \ n", lpbysrc [I]);
Else
Wsprintf (szbyte, "% 02x", lpbysrc [I]);
Lstrcat (lpdst, szbyte );
}
// Process the last byte
Wsprintf (szbyte, "% 02x", lpbysrc [I]);
Lstrcat (lpdst, szbyte );
Free (lpbysrc );
Lpbysrc = NULL;
Closehandle (hfilesrc );
// Save it to the target file
Hfiledst = createfile (szfiledst, generic_write, 0, null, create_always, 0, null );
Writefile (hfiledst, (lpcvoid) lpdst, lstrlen (lpdst) * sizeof (tchar), & dwreaded, null );
Free (lpdst );
Lpdst = NULL;
Closehandle (hfiledst );
}
When I put this code into the array, I found that although it can work normally, the result is also what I want, but it is surprisingly slow in processing files, even if the file size difference is several dozen K, there will be a significant speed gap! I browsed my code again and found no fatal errors. I had a chance and thought I was using a GUI, so I didn't have much effort. I added a few lines of code and a progress bar in this thread to continue compilation and running.
As a result, I found that the progress bar indicating the progress of byte processing is slower. I suddenly realized that the strcat source code included with VC was opened:
Char * _ cdecl strcat (char * DST, const char * SRC)
{
Char * CP = DST;
While (* CP)
CP ++;/* Find end of DST */
While (* CP ++ = * SRC ++);/* Copy SRC to end of DST */
Return (DST);/* return DST */
}
This process is clear: first find the end character at the end of the string, and then copy the string. In my code, every time a loop is completed, lstrcat will not bother looking for an Terminator, and then copy it again-this will cause a lot of useless work, that is why the progress bar slows down.
After knowing the cause of the injury, I decided to change the space for time-use a variable to point to the end of the target string to manually connect the string. So I wrote the following code:
Void save (void)
{
DWORD dwsize, dwreaded, I, J, K;
Tchar szbyte [5];
// Read the source file
Hfilesrc = createfile (szfilesrc, generic_read, 0, null, open_always, 0, null );
Dwsize = getfilesize (hfilesrc, null );
Lpbysrc = new (byte, dwsize );
Readfile (hfilesrc, (lpvoid) lpbysrc, dwsize, & dwreaded, null );
// The following mysize is a macro that indicates the buffer size. Because the calculation size is cumbersome and irrelevant to this article, I will skip it here
Lpdst = new (tchar, mysize );
* Lpdst = '\ 0 ';
J = 0;
For (I = 0; I <dwsize-1; I ++)
{
If (I % 16 = 15) // process line feed
{
Wsprintf (szbyte, "% 02x \ r \ n", lpbysrc [I]);
K = 4;
}
Else
{
Wsprintf (szbyte, "% 02x", lpbysrc [I]);
K = 3;
}
Lstrcpy (& lpdst [J], szbyte );
J + = K;
}
// Process the last byte
Wsprintf (szbyte, "% 02x", lpbysrc [I]);
Lstrcpy (& lpdst [J], szbyte );
Free (lpbysrc );
Lpbysrc = NULL;
Closehandle (hfilesrc );
// Save it to the target file
Hfiledst = createfile (szfiledst, generic_write, 0, null, create_always, 0, null );
Writefile (hfiledst, (lpcvoid) lpdst, lstrlen (lpdst) * sizeof (tchar), & dwreaded, null );
Free (lpdst );
Lpdst = NULL;
Closehandle (hfiledst );
}
Writing the code here should end, but this topic is indeed worth mentioning-it can be said that it leads to the above troublesome "remark ", it is the "zero-end" mechanism of the C-style string itself. Then I will list a piece of code for your product:
Void cstring: concatcopy (INT nsc1len, lpctstr lpszsrc1data, int nsc2len, lpctstr lpszsrc2data)
{
// -- Master concatenation routine
// Concatenate two sources
// -- Assume that 'eas' is a new cstring object
Int nnewlen = nsc1len + nsc2len;
If (nnewlen! = 0)
{
Allocbuffer (nnewlen );
Memcpy (m_pchdata, lpszsrc1data, nsc1len * sizeof (tchar ));
Memcpy (m_pchdata + nsc1len, lpszsrc2data, nsc2len * sizeof (tchar ));
}
}
As you can see, this is the cstring source code snippet In the MFC framework. Cstring uses memcpy instead of strcat/lstrcat as its connection function to avoid embarrassment caused by searching for the end, and the string length given by the parameter directly determines the tail position of the string. So can we use cstring: Operator + = to complete the above operations?
The answer is no. I did say that cstring avoids the embarrassment of searching for the end, but cstring brings another embarrassment-the embarrassment of duplicate replication. Cstring: Operator + = in the final analysis, the above cstring: concatcopy is called, And once cstring: concatcopy is called, memcpy is called twice, so cstring :: operator + = is a more cost-effective method.
Whether it is a string processing function of C or a string class constructed using C ++, it can be regarded as a "black box ". In general, users do not need to understand the internal implementation mechanism of the black box, as long as the "black box" is perfect and can be used directly. In fact, the black box itself is not perfect and omnipotent-even if the Black Box is a C/C ++ standard library, the mistakes that may make you confused may be hidden behind the seemingly perfect black box.