Last active
October 31, 2022 23:17
-
-
Save 0xPIT/cbfe5b91dff41576f0c795cf4135e60e to your computer and use it in GitHub Desktop.
Simple hex + ASCII dump
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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