C语言 hexdump
打印效果如图所示
#include <stdio.h>
#include <ctype.h> // isprint
void hexDump(const unsigned char *data, int size)
{
int i, offset;
for (offset = 0; offset < size; offset += 16)
{
// 打印地址
printf("%04x ", offset);
// 打印中间十六进制数据
for (i = 0; i < 16; i++)
{
if (i % 8 == 0)
{
putchar(' ');
}
if (offset + i < size)
{
printf("%02x ", data[offset + i]);
}
else
{
printf(" ");
}
}
printf(" ");
// 打印可显示字符
for (i = 0; i < 16 && offset + i < size; i++)
{
if (isprint(data[offset + i]))
{
printf("%c", data[offset + i]);
}
else
{
putchar('.');
}
}
putchar('\n');
}
}
int main()
{
int i = 0;
unsigned char tmp[0xff] = {0};
for (i = 0; i < sizeof(tmp); i++)
{
tmp[i] = i;
}
hexDump(tmp, sizeof(tmp));
return 0;
}