Skip to content

Instantly share code, notes, and snippets.

@maxrt101
Created March 2, 2023 03:04
Show Gist options
  • Save maxrt101/def375a140d5769e971e2eea3797f4dd to your computer and use it in GitHub Desktop.
Save maxrt101/def375a140d5769e971e2eea3797f4dd to your computer and use it in GitHub Desktop.
Hexdump function
#include <cstring>
#include <cctype>
#include <cstdio>
void hexdump(const unsigned char* data, size_t size, int cols) {
int can_print = 1;
size_t count = 0;
char* buf = new char[cols];
while (can_print) {
memset(buf, '.', cols);
printf("0x%04zx |", count);
for (int i = 0; i < cols; i++) {
printf("%s", (i % (cols/2) == 0 && i != 0) ? " " : " ");
if (count < size) {
printf("%02x", (unsigned char) data[count]);
buf[count % cols] = data[count];
count++;
} else {
printf(" ");
can_print = 0;
}
}
printf(" | ");
for (int i = 0; i < cols; i++) {
printf("%c", isprint(buf[i]) ? buf[i] : '.');
}
printf("\n");
}
delete [] buf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment