Created
December 12, 2012 09:15
-
-
Save chmike/4266308 to your computer and use it in GitHub Desktop.
Print binary data in haxadecimal
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
#ifndef PRINTHEX_H | |
#define PRINTHEX_H | |
#include <stdio.h> | |
// Display data in hexadecimal | |
inline void printHex( const void* data, size_t len, const char* margin ) | |
{ | |
if( len == 0 ) | |
{ | |
if( margin ) | |
printf( "%s", margin ); | |
printf("\n"); | |
return; | |
} | |
int x = 0; | |
size_t cnt = 0; | |
char ascii[32], *ap; | |
const unsigned char* p = (unsigned char*)data; | |
while( len-- ) | |
{ | |
if( x == 0 ) | |
{ | |
if( margin ) | |
printf( "%s", margin ); | |
printf( "%6.06lld ", (unsigned long long)cnt ); | |
ap = ascii; | |
} | |
if( x++ == 8 ) | |
printf( " " ); | |
printf( "%02X ", *p ); | |
*ap = ( *p >= ' ' && *p < 127 ) ? *p : '.'; | |
if( x == 16 ) | |
{ | |
*ap = '\0'; | |
printf( " %s\n", ascii ); | |
x = 0; | |
} | |
p++; | |
ap++; | |
cnt++; | |
} | |
if( x > 0 ) | |
{ | |
if( x < 8 ) | |
printf( " " ); | |
while( x++ < 16 ) | |
printf( " " ); | |
*ap = '\0'; | |
printf( " %s\n", ascii ); | |
} | |
if( margin ) | |
printf( "%s", margin ); | |
printf( "%6.06lu\n", cnt ); | |
} | |
#endif // PRINTHEX_H | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment