CPlay/freelist/hexdump.c
2025-09-09 10:05:29 +02:00

35 lines
869 B
C

#include <ctype.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
void hexdump(const void *data, size_t size) {
const unsigned char *p = (const unsigned char *)data;
size_t i, j;
for (i = 0; i < size; i += 16) {
// Print offset
printf("%08zx ", i);
// Print hex bytes
for (j = 0; j < 16; j++) {
if (i + j < size) {
printf("%02X ", p[i + j]);
} else {
printf(" "); // padding for incomplete lines
}
if (j == 7)
printf(" "); // extra space in middle
}
printf(" |");
// Print ASCII characters
for (j = 0; j < 16 && i + j < size; j++) {
unsigned char c = p[i + j];
printf("%c", isprint(c) ? c : '.');
}
printf("|\n");
}
}