-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexdump.c
33 lines (32 loc) · 1.07 KB
/
hexdump.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "hexdump.h"
void hexdump(void *buf, size_t sz) {
for (size_t i = 0; i < sz / 16; i++) {
printf("%08zx ", i * 16);
for (size_t j = 0; j < 16; j++) {
if(j == 8) printf(" ");
printf("%02x ", ((uint8_t *)buf)[i * 16 + j]);
}
printf(" |");
for (size_t j = 0; j < 16; j++) {
printf("%c", isprint(((uint8_t *)buf)[i * 16 + j]) ? ((uint8_t *)buf)[i * 16 + j] : '.');
}
printf("|\n");
}
if (sz % 16) {
printf("%08zx ", sz / 16 * 16);
for (size_t j = 0; j < sz % 16; j++) {
if(j == 8) printf(" ");
printf("%02x ", ((uint8_t *)buf)[sz / 16 * 16 + j]);
}
for (size_t j = 0; j < 16 - sz % 16; j++) {
if(sz % 16 + j == 8) printf(" ");
printf(" ");
}
printf(" |");
for (size_t j = 0; j < sz % 16; j++) {
printf("%c", isprint(((uint8_t *) buf)[sz / 16 * 16 + j]) ? ((uint8_t *) buf)[sz / 16 * 16 + j] : '.');
}
printf("|\n");
}
printf("%08zx\n", sz);
}