Skip to content

Instantly share code, notes, and snippets.

@chmike
Created December 12, 2012 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chmike/4266308 to your computer and use it in GitHub Desktop.
Save chmike/4266308 to your computer and use it in GitHub Desktop.
Print binary data in haxadecimal
#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