Created
March 2, 2023 11:27
-
-
Save polprog/0d85bd9e61734ad993b420fa3d322fbc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** hexdump.h */ | |
/* | |
Hexdump functions | |
polprog 2021. Released on 3-clause BSD | |
*/ | |
#ifndef _HEXDUMP_H | |
#define _HEXDUMP_H | |
void hexdump(char *data, size_t len); | |
void hexdump8(char *data, size_t len); | |
#endif // _HEXDUMP_H | |
/** hexdump.c */ | |
/* | |
Hexdump functions | |
polprog 2021. Released on 3-clause BSD | |
*/ | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <unistd.h> | |
#include <string.h> | |
void hexdump(char *data, size_t len){ | |
size_t i = 0; | |
putchar('\n'); | |
printf("addr +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +a +b +c +d +e +f 0123456789abcdef\n"); | |
for(i = 0; i < len;){ | |
if(i % 16 == 0) printf("0x%04x ", i); | |
printf("%02x ", data[i]); | |
i++; | |
if(i % 16 == 0){ | |
for(size_t j = 16; j >= 1; j--){ | |
putchar((data[i-j] < 32 || data[i-j] > 127) ? '.' : data[i-j]); | |
} | |
putchar('\n'); | |
} | |
} | |
for(size_t j = 0; j < 16-i%16; j++) printf(" "); | |
for(size_t j = i%16; j >= 1; j--){ | |
putchar((data[i-j] < 32 || data[i-j] > 127) ? '.' : data[i-j]); | |
} | |
putchar('\n'); | |
} | |
void hexdump8(char *data, size_t len){ | |
size_t i = 0; | |
putchar('\n'); | |
printf("addr +0 +1 +2 +3 +4 +5 +6 +7 01234567\n"); | |
for(i = 0; i < len;){ | |
if(i % 8 == 0) printf("0x%04x ", i); | |
printf("%02x ", data[i]); | |
i++; | |
if(i % 8 == 0){ | |
for(size_t j = 8; j >= 1; j--){ | |
putchar((data[i-j] < 32 || data[i-j] > 127) ? '.' : data[i-j]); | |
} | |
putchar('\n'); | |
} | |
} | |
for(size_t j = 0; j < 8-i%8; j++) printf(" "); | |
for(size_t j = i%8; j >= 1; j--){ | |
putchar((data[i-j] < 32 || data[i-j] > 127) ? '.' : data[i-j]); | |
} | |
putchar('\n'); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment