Last active
May 1, 2022 22:33
-
-
Save LeoDJ/f657ec3365c8b339cda282b005544275 to your computer and use it in GitHub Desktop.
Print hexdump of a buffer. Quickly hacked together but works.
This file contains hidden or 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
// Quick way to print a hexdump of a buffer. Output will look like this: | |
// | |
// 0 1 2 3 4 5 6 7 8 9 A B C D E F | |
// 0000 54 68 69 73 20 69 73 20 61 20 74 65 73 74 20 68 This is a test h | |
// 0010 65 78 64 75 6D 70 21 20 46 6F 6F 2E 20 01 02 03 exdump! Foo. ... | |
// 0020 04 . | |
inline void printHexdumpAscii(uint16_t index, uint8_t byte) { | |
if (index % 16 == 8) { // some spacing for orientation | |
printf(" "); | |
} | |
if (byte < 32) { | |
printf("."); | |
} | |
else { | |
printf("%c", byte); | |
} | |
} | |
void printHexdump(uint8_t* buf, uint16_t size) { | |
printf(" "); // empty space in top left corner | |
for(uint8_t i = 0; i < 16; i++) { // print table column header | |
printf("%1X ", i); | |
} | |
printf("\n%04X ", 0); // print first row header | |
for(uint16_t i = 0; i < size; i++) { | |
printf("%02X ", buf[i]); | |
if(i % 16 == 15) { // end of row | |
// print ascii representation | |
printf(" "); | |
for (uint16_t j = i - 15; j <= i; j++) { | |
printHexdumpAscii(j, buf[j]); | |
} | |
printf("\n%04X ", i+1); // print row header | |
} | |
} | |
// print ASCII of last row | |
if (size % 16 != 0) { | |
uint16_t remaining = 16 - (size % 16); | |
for (uint16_t i = 0; i < remaining; i++) { // fill up empty space of last row | |
printf(" "); | |
} | |
printf(" "); | |
for (uint16_t i = size - (size % 16); i < size; i++) { | |
printHexdumpAscii(i, buf[i]); | |
} | |
} | |
printf("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment