如下代码(支持windows与Linux)会以【16进制】【每行16字节】打印出一块内存的内容:

void PrintBuffer(void* pBuff, unsigned int nLen)
{
if (NULL == pBuff || == nLen)
{
return;
} const int nBytePerLine = ;
unsigned char* p = (unsigned char*)pBuff;
char szHex[*nBytePerLine+] = {}; printf("-----------------begin-------------------\n");
for (unsigned int i=; i<nLen; ++i)
{
int idx = * (i % nBytePerLine);
if ( == idx)
{
memset(szHex, , sizeof(szHex));
}
#ifdef WIN32
sprintf_s(&szHex[idx], , "%02x ", p[i]);// buff长度要多传入1个字节
#else
snprintf(&szHex[idx], , "%02x ", p[i]); // buff长度要多传入1个字节
#endif // 以16个字节为一行,进行打印
if ( == ((i+) % nBytePerLine))
{
printf("%s\n", szHex);
}
} // 打印最后一行未满16个字节的内容
if ( != (nLen % nBytePerLine))
{
printf("%s\n", szHex);
} printf("------------------end-------------------\n");
}

打印出来的结果如下:

以16进制打印出一块内存buff-LMLPHP

05-07 15:30