Created
March 2, 2023 03:04
-
-
Save maxrt101/def375a140d5769e971e2eea3797f4dd to your computer and use it in GitHub Desktop.
Hexdump function
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
#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