Skip to content

Instantly share code, notes, and snippets.

@igorsobreira
Created May 6, 2012 02:39
Show Gist options
  • Save igorsobreira/2607166 to your computer and use it in GitHub Desktop.
Save igorsobreira/2607166 to your computer and use it in GitHub Desktop.
Hexdump, useful to debug C code. From ##c irc channel
/* HEXDUMP */
#include <stddef.h>
#include <stdio.h>
#include <ctype.h>
void hexdump(const void * memory, size_t bytes) {
const unsigned char * p, * q;
int i;
p = memory;
while (bytes) {
q = p;
printf("%p: ", (void *) p);
for (i = 0; i < 16 && bytes; ++i) {
printf("%02X ", *p);
++p;
--bytes;
}
bytes += i;
while (i < 16) {
printf("XX ");
++i;
}
printf("| ");
p = q;
for (i = 0; i < 16 && bytes; ++i) {
printf("%c", isprint(*p) && !isspace(*p) ? *p : ' ');
++p;
--bytes;
}
while (i < 16) {
printf(" ");
++i;
}
printf(" |\n");
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment