hexdump.[ch]: utility function for dumping memory regions as hex
This commit is contained in:
parent
f4342ab221
commit
1ed4b57882
3 changed files with 44 additions and 1 deletions
34
kern/libkern/hexdump.c
Normal file
34
kern/libkern/hexdump.c
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#include <stddef.h>
|
||||
#include <ctype.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
|
||||
kprintf("%08zx ", i);
|
||||
|
||||
// Print hex bytes
|
||||
for (j = 0; j < 16; j++) {
|
||||
if (i + j < size) {
|
||||
kprintf("%02X ", p[i + j]);
|
||||
} else {
|
||||
kprintf(" "); // padding for incomplete lines
|
||||
}
|
||||
if (j == 7)
|
||||
kprintf(" "); // extra space in middle
|
||||
}
|
||||
|
||||
kprintf(" |");
|
||||
|
||||
// Print ASCII characters
|
||||
for (j = 0; j < 16 && i + j < size; j++) {
|
||||
unsigned char c = p[i + j];
|
||||
kprintf("%c", isprint(c) ? c : '.');
|
||||
}
|
||||
|
||||
kprintf("|\n");
|
||||
}
|
||||
}
|
||||
8
kern/libkern/hexdump.h
Normal file
8
kern/libkern/hexdump.h
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef HEXDUMP_H
|
||||
#define HEXDUMP_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void hexdump(const void *data, size_t size);
|
||||
|
||||
#endif // HEXDUMP_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue