Created
August 5, 2017 02:55
-
-
Save leslie-wang/10fe63f95f66c5947cac6208c411efc2 to your computer and use it in GitHub Desktop.
Dump buffer in hex and dec mode as vi hex mode
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 buffer_dump(char *data, int length) { | |
int row, col, idx = 0; | |
for (row = 0; row <= length / 16; row++) { | |
int len = row == length / 16 ? length % 16 : 16; | |
printf("%d: ", row); | |
//print hex | |
for (col = 0; col < len; col++) { | |
printf("%02X ", (unsigned char) data[idx + col]); | |
} | |
for (; col < 16; col++) | |
printf(" "); | |
//print split string | |
printf(" "); | |
//print dec | |
for (col = 0; col < len; col++) { | |
printf("%c", (data[idx + col] >= ' ' && data[idx + col] <= '~') ? data[idx + col] : '.'); | |
} | |
printf("\n"); | |
idx += 16; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment