Skip to content

Instantly share code, notes, and snippets.

@bakulev
Created November 18, 2019 08:10
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 bakulev/1b95f1a4eaea7c8794c0afcfc4fe7c1d to your computer and use it in GitHub Desktop.
Save bakulev/1b95f1a4eaea7c8794c0afcfc4fe7c1d to your computer and use it in GitHub Desktop.
print hex dump of memory
void hexDump (char *desc, void *addr, int len) {
int i;
unsigned char buff[17]; // stores the ASCII data
unsigned char *pc = addr; // cast to make the code cleaner.
// Output description if given.
if (desc != NULL)
printf ("%s:\n", desc);
// Process every byte in the data.
for (i = 0; i < len; i++) {
// Multiple of 16 means new line (with line offset).
if ((i % 16) == 0) {
// Just don't print ASCII for the zeroth line.
if (i != 0)
printf (" %s\n", buff);
// Output the offset.
printf (" %p ", addr + i);
}
// Now the hex code for the specific character.
printf (" %02x", pc[i]);
// And store a printable ASCII character for later.
if ((pc[i] < 0x20) || (pc[i] > 0x7e))
buff[i % 16] = '.';
else
buff[i % 16] = pc[i];
buff[(i % 16) + 1] = '\0';
}
// Pad out last line if not exactly 16 characters.
while ((i % 16) != 0) {
printf (" ");
i++;
}
// And print the final ASCII bit.
printf (" %s\n", buff);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment