Prints out a memory buff in hexadecimal notation.

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.