编码过程中我们有时候需要将数据的十六进制值打出来看看,这里做一下记录
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
void pretty_print(uint8_t* ret,size_t size)
{
for (size_t i = 0; i < size; i++)
{
size_t c = i;
printf("%08zx ",i);
for (size_t j = 0; j < 8; j++, c++)
{
if (c < size)
{
printf("%02X ", ret[c]);
}
else
{
printf(" ");
}
}
printf(" ");
for (size_t j = 0; j < 8; j++, c++)
{
if (c < size)
{
printf("%02X ", ret[c]);
}
else
{
printf(" ");
}
}
c = i;
printf(" |");
for (size_t j = 0; j < 16 && c < size; j++, c++)
{
if (isprint(ret[c]) && ret[c])
{
printf("%c", ret[c]);
}
else
{
printf(".");
}
}
i = i + c - 1;
printf("|\n");
}
printf("%08zx\n",size);
}
标签:Hexdump,++,代码,ret,else,printf,include,size
From: https://www.cnblogs.com/SupperMary/p/17740941.html