Skip to content

Instantly share code, notes, and snippets.

@0xPIT
Last active October 31, 2022 23:17
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 0xPIT/cbfe5b91dff41576f0c795cf4135e60e to your computer and use it in GitHub Desktop.
Save 0xPIT/cbfe5b91dff41576f0c795cf4135e60e to your computer and use it in GitHub Desktop.
Simple hex + ASCII dump
void hexdump(const void* data, size_t size) {
size_t j;
size_t i;
#define nexti (i + 1)
const uint8_t wrap = 16;
char ascii[wrap + 1];
memset(ascii, 0, sizeof(ascii));
for (i = 0; i < size; ++i) {
printf("%02X ", ((uint8_t *)data)[i]);
if (((uint8_t *)data)[i] >= ' ' && ((uint8_t *)data)[i] <= '~') {
ascii[i % wrap] = ((uint8_t *)data)[i];
}
else {
ascii[i % wrap] = '.';
}
if (nexti % 8 == 0 || nexti == size) {
printf(" ");
if (nexti % wrap == 0) {
printf("| %-*s |\n", wrap, ascii);
}
else if (nexti == size) {
ascii[nexti % wrap] = '\0';
if (nexti % wrap <= 8) {
printf(" ");
}
for (j = nexti % wrap; j < wrap; ++j) {
printf(" ");
}
printf("| %-*s |\n", wrap, ascii);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment