forked from battlesnake/tun-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhexdump.c
More file actions
36 lines (34 loc) · 659 Bytes
/
hexdump.c
File metadata and controls
36 lines (34 loc) · 659 Bytes
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
34
35
36
#include <stdio.h>
#include <stddef.h>
#define STEP 16
void hexdump(const char *title, const void *p, size_t len)
{
const char *cp = p;
printf("%s\n", title);
for (size_t i = 0; i < len ; i += STEP) {
printf("%04zx |", i);
for (size_t j = 0; j < STEP; j++) {
size_t k = i + j;
if (j % 4 == 0) {
printf(" ");
}
if (k < len) {
printf(" %02hhx", cp[k]);
} else {
printf(" ");
}
}
printf(" |");
for (size_t j = 0; j < STEP; j++) {
size_t k = i + j;
char c = k < len ? cp[k] : ' ';
c = c <= 32 ? '.' : c;
if (j % 4 == 0) {
printf(" ");
}
printf("%c", c);
}
printf("\n");
}
printf("\n");
}