闲来无事,为了练练c编程,于是乎就写了以下这个函数:
/***********************************************************************************
3 * Function Name : print_hex_ascii
4 *
5 * Parameter: buff ---- The head pointer of the data buffer
6 * len ---- The total length of the data buffer
7 * offset ---- The offset of the buffer
8 *
9 * Description: print the data in the 'buff' with both hex and ascii
10 * eg.
11 * 0022ff00 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?
12 * 0022ff10 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
13 * 0022ff20 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[/]^_
14 *
15 * Return : NONE
16 *
17 **********************************************************************************/
18
19 void print_hex_ascii(const u8 *buff,int len,int offset)
20 {
21 int total_line;
22 int i,j,index;
23 #define LENGTH_PER_LINE 16
24
25 if(offset>=len)
26 return;
27
28 total_line = (len+(LENGTH_PER_LINE-1))/LENGTH_PER_LINE;
29
30 for(i=0; i<total_line; i++)
31 {
32 /* print base address */
33 printf("%08x ",(int)buff+i*LENGTH_PER_LINE);
34
35 /* print hex in every line */
36 for(j=0; j<LENGTH_PER_LINE; j++)
37 {
38 index = i*LENGTH_PER_LINE+j;
39
40 if ( (index >= len) || (index < offset) )
41 {
42 printf(" ");
43 }
44 else
45 {
46 printf("%02x ",buff[index]);
47 }
48
49 if(j%4==3)
50 printf(" ");
51 }
52
53 printf(" "); //The gap between hex and ascii;
54
55 /* print ascii after the hex */
56 for(j=0; j<LENGTH_PER_LINE; j++)
57 {
58 index = i*LENGTH_PER_LINE+j;
59
60 if( (index>=len) || (index<offset) )
61 {
62 printf(" ");
63 }
64 else
65 {
66 if( isprint(buff[index]) )
67 printf("%c", buff[index]);
68 else if(index<len)
69 printf(".");
70 }
71 }
72
73 printf("/n");
74 }
75 }