/* * printhex.c * I)ruid * * Function to print a buffer as hex with ascii representations. * */ #include #include void printhex( unsigned char *buf, int size ) { int x, y; for( x=1; x<=size; x++ ) { if( x == 1 ) printf( "%04x ", x-1 ); /* Print an offset line header */ printf( "%02x ", buf[x-1] ); /* print the hex value */ if( x % 8 == 0 ) printf( " " ); /* padding space at 8 and 16 bytes */ if( x % 16 == 0 ) { /* We're at the end of a line of hex, print the printables */ printf( " " ); for( y = x - 15; y <= x; y++ ) { if( isprint( buf[y-1] ) ) printf( "%c", buf[y-1] ); /* if it's printable, print it */ else printf( "." ); /* otherwise substitute a period */ if( y % 8 == 0 ) printf( " " ); /* 8 byte padding space */ } if( x < size ) printf( "\n%04x ", x ); /* Print an offset line header */ } } x--; /* If we didn't end on a 16 byte boundary, print some placeholder spaces before printing ascii */ if( x % 16 != 0 ) { for( y = x+1; y <= x + (16-(x % 16)); y++ ) { printf( " " ); /* hex value placeholder spaces */ if( y % 8 == 0 ) printf( " " ); /* 8 and 16 byte padding spaces */ }; /* print the printables */ printf( " " ); for( y = (x+1) - (x % 16); y <= x; y++ ) { if( isprint( buf[y-1] ) ) printf( "%c", buf[y-1] ); /* if it's printable, print it */ else printf( "." ); /* otherwise substitute a period */ if( y % 8 == 0 ) printf( " " ); /* 8 and 16 byte padding space */ } } /* Done! */ printf( "\n" ); }