一个十六进制格式化输出:
#include <stdio.h>
#include <stdlib.h>
#include <string.h> void hexdump( void *data, unsigned int len )
{
char str[], octet[];
int offset, i, k;
unsigned char *buf = (unsigned char *)data;
char dimm[] = "+------------------------------------------------------------------------------+"; printf("%s\r\n", dimm);
printf("| Offset : 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 0123456789ABCDEF |\r\n");
printf("%s\r\n", dimm); for (offset = ; offset < len; offset += ) {
sprintf( str, "| %08x: ", offset ); for (i = ; i < ; i++) {
if ((i + offset) < len)
sprintf( octet, "%02x ", buf[offset + i] );
else
strcpy( octet, " " ); strcat( str, octet );
}
strcat( str, " " );
k = strlen( str ); for (i = ; i < ; i++) {
if ((i + offset) < len)
str[k++] = (0x20 <= (buf[offset + i]) && (buf[offset + i]) <= 0x7E) ? buf[offset + i] : '.';
else
str[k++] = ' ';
} str[k] = '\0';
printf("%s |\r\n", str);
} printf("%s\r\n", dimm);
} int main( void )
{
int i;
unsigned char test_buf[]; for( i = ; i < sizeof(test_buf)/sizeof(test_buf[]); i++ )
{
test_buf[i] = i % ;
} hexdump( test_buf, sizeof(test_buf)/sizeof(test_buf[]) ); return ;
}