Skip to content

Instantly share code, notes, and snippets.

@droogie
Forked from iljavs/hexdump.c
Created July 21, 2022 16:58
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 droogie/508c4fb9cbd44878930f44acb833a4b0 to your computer and use it in GitHub Desktop.
Save droogie/508c4fb9cbd44878930f44acb833a4b0 to your computer and use it in GitHub Desktop.
void hexdump(unsigned char *data, size_t size) {
char ascii[17] = {0};
size_t i;
for (i = 0; i < size; ++i) {
unsigned char c = data[i];
size_t next = i+1;
printf("%02X ", c);
ascii[i % 16] = isprint(c) ? c : '.';
if (next % 8 == 0 || next == size) {
printf(" ");
if (next % 16 == 0) {
printf("| %s \n", ascii);
}
else if (next == size) {
size_t j;
ascii[size % 16] = '\0';
if (size % 16 <= 8) {
printf(" ");
}
for (j = size % 16; j < 16; ++j) {
printf(" ");
}
printf("| %s \n", ascii);
}
}
fflush(stdout);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment