The following code (Windows and Linux are supported) prints a piece of memory in hexadecimal notation (16 bytes per line:
Void printbuffer (void * pbuff, unsigned int nlen) {If (null = pbuff | 0 = nlen) {return;} const int nbyteperline = 16; unsigned char * P = (unsigned char *) pbuff; char szhex [3 * nbyteperline + 1] = {0}; printf ("----------------- begin ------------------- \ n "); for (unsigned int I = 0; I <nlen; ++ I) {int idx = 3 * (I % nbyteperline); If (0 = idx) {memset (szhex, 0, sizeof (szhex);} # ifdef Win32 sprintf_s ( & Szhex [idx], 4, "% 02x", P [I]); // The buff length must be 1 byte more # else snprintf (& szhex [idx], 4, "% 02x", P [I]); // The buff length must be 1 byte more # endif // use 16 bytes as a line, print if (0 = (I + 1) % nbyteperline) {printf ("% s \ n", szhex );}} // print if (0! = (Nlen % nbyteperline) {printf ("% s \ n", szhex);} printf ("------------------ end ------------------- \ n ");}
The output is as follows: