hexdump
Posted uTank-木头
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hexdump相关的知识,希望对你有一定的参考价值。
一个十六进制格式化输出:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 6 void hexdump( void *data, unsigned int len ) 7 { 8 char str[160], octet[10]; 9 int offset, i, k; 10 unsigned char *buf = (unsigned char *)data; 11 char dimm[] = "+------------------------------------------------------------------------------+"; 12 13 printf("%s\r\n", dimm); 14 printf("| Offset : 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 0123456789ABCDEF |\r\n"); 15 printf("%s\r\n", dimm); 16 17 for (offset = 0; offset < len; offset += 16) { 18 sprintf( str, "| %08x: ", offset ); 19 20 for (i = 0; i < 16; i++) { 21 if ((i + offset) < len) 22 sprintf( octet, "%02x ", buf[offset + i] ); 23 else 24 strcpy( octet, " " ); 25 26 strcat( str, octet ); 27 } 28 strcat( str, " " ); 29 k = strlen( str ); 30 31 for (i = 0; i < 16; i++) { 32 if ((i + offset) < len) 33 str[k++] = (0x20 <= (buf[offset + i]) && (buf[offset + i]) <= 0x7E) ? buf[offset + i] : ‘.‘; 34 else 35 str[k++] = ‘ ‘; 36 } 37 38 str[k] = ‘\0‘; 39 printf("%s |\r\n", str); 40 } 41 42 printf("%s\r\n", dimm); 43 } 44 45 int main( void ) 46 { 47 int i; 48 unsigned char test_buf[333]; 49 50 for( i = 0; i < sizeof(test_buf)/sizeof(test_buf[0]); i++ ) 51 { 52 test_buf[i] = i % 256; 53 } 54 55 hexdump( test_buf, sizeof(test_buf)/sizeof(test_buf[0]) ); 56 57 return 0; 58 }
以上是关于hexdump的主要内容,如果未能解决你的问题,请参考以下文章