Skip to content

Instantly share code, notes, and snippets.

@neesenk
Created November 27, 2010 15:57
Show Gist options
  • Save neesenk/718003 to your computer and use it in GitHub Desktop.
Save neesenk/718003 to your computer and use it in GitHub Desktop.
16进制打印内存的内容
/* 0000000: 0000 0000 0000 0000 0000 0000 0000 0000 ................ */
void hexdump(const char *buff, int blen)
{
static char hexmap[] = "0123456789abcdef";
int offset, len, count, hexoff;
unsigned char str[58], c;
str[57] = 0, str[40] = ' ';
for (offset = 0, len = 16; offset < blen; offset += 16) {
if (blen - offset < 16) {
len = blen - offset;
memset(str, ' ', 57);
}
for (count = 0, hexoff = 0; count < len; count++) {
c = *(unsigned char *)(buff + offset + count);
str[hexoff++] = hexmap[(c >> 4) & 0xf];
str[hexoff++] = hexmap[c & 0xf];
if (count % 2)
str[hexoff++] = ' ';
str[41 + count] = isprint(c) ? c : '.';
}
printf("%07x: %s\n", offset, str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment