Skip to content

Instantly share code, notes, and snippets.

@elazarl
Created August 8, 2016 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elazarl/6640b68891ce340c5c865d0ad9b1b60a to your computer and use it in GitHub Desktop.
Save elazarl/6640b68891ce340c5c865d0ad9b1b60a to your computer and use it in GitHub Desktop.
C hexdump in objdump -x format
#include <ctype.h>
#include <stdio.h>
struct buf {
unsigned char *p;
uint64_t len;
};
void hexdump(struct buf buf) {
int i = 0;
printf(" 0x%08x ", i);
for (; i < buf.len; i++) {
if (i && (i % 16) == 0) {
int j;
printf(" ");
for (j = i - 16; j < i; j++)
printf("%c", isprint(buf.p[j]) ? buf.p[j] : '.');
printf("\n 0x%08x ", i);
} else if (i && (i % 4) == 0)
printf(" ");
printf("%02x", buf.p[i]);
}
puts("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment