Created
October 2, 2016 08:19
-
-
Save kostyll/bafe3a3e8e221418f63522ac69624446 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
#include <ctype.h> | |
#include <stdio.h> | |
void hexdump(void *mem, unsigned int len, int cols_count) | |
{ | |
unsigned int i, j; | |
for(i = 0; i < len + ((len % cols_count) ? (cols_count - len % cols_count) : 0); i++) | |
{ | |
/* print offset */ | |
if(i % cols_count == 0) | |
{ | |
printf("0x%06x: ", i); | |
} | |
/* print hex data */ | |
if(i < len) | |
{ | |
printf("%02x ", 0xFF & ((char*)mem)[i]); | |
} | |
else /* end of block, just aligning for ASCII dump */ | |
{ | |
printf(" "); | |
} | |
/* print ASCII dump */ | |
if(i % cols_count == (cols_count - 1)) | |
{ | |
for(j = i - (cols_count - 1); j <= i; j++) | |
{ | |
if(j >= len) /* end of block, not really printing */ | |
{ | |
putchar(' '); | |
} | |
else if(isprint(((char*)mem)[j])) /* printable char */ | |
{ | |
putchar(0xFF & ((char*)mem)[j]); | |
} | |
else /* other char */ | |
{ | |
putchar('.'); | |
} | |
} | |
putchar('\n'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment