Skip to content

Instantly share code, notes, and snippets.

@leslie-wang
Created August 5, 2017 02:55
Show Gist options
  • Save leslie-wang/10fe63f95f66c5947cac6208c411efc2 to your computer and use it in GitHub Desktop.
Save leslie-wang/10fe63f95f66c5947cac6208c411efc2 to your computer and use it in GitHub Desktop.
Dump buffer in hex and dec mode as vi hex mode
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