Skip to content

Instantly share code, notes, and snippets.

@sunnyc7
Forked from ccbrown/DumpHex.c
Created July 13, 2023 18:06
Show Gist options
  • Save sunnyc7/c2764794f76c9e6d80fd3de6bc9b3f13 to your computer and use it in GitHub Desktop.
Save sunnyc7/c2764794f76c9e6d80fd3de6bc9b3f13 to your computer and use it in GitHub Desktop.
Compact C Hex Dump Function w/ASCII
#include <stdio.h>
void DumpHex(const void* data, size_t size) {
char ascii[17];
size_t i, j;
ascii[16] = '\0';
for (i = 0; i < size; ++i) {
printf("%02X ", ((unsigned char*)data)[i]);
if (((unsigned char*)data)[i] >= ' ' && ((unsigned char*)data)[i] <= '~') {
ascii[i % 16] = ((unsigned char*)data)[i];
} else {
ascii[i % 16] = '.';
}
if ((i+1) % 8 == 0 || i+1 == size) {
printf(" ");
if ((i+1) % 16 == 0) {
printf("| %s \n", ascii);
} else if (i+1 == size) {
ascii[(i+1) % 16] = '\0';
if ((i+1) % 16 <= 8) {
printf(" ");
}
for (j = (i+1) % 16; j < 16; ++j) {
printf(" ");
}
printf("| %s \n", ascii);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment