20分 |
仅供参考:
void HexDump(char *buf,int len,int addr) { int i,j,k; char binstr[80]; for (i=0;i<len;i++) { if (0==(i%16)) { sprintf(binstr,"%08x -",i+addr); sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]); } else if (15==(i%16)) { sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]); sprintf(binstr,"%s ",binstr); for (j=i-15;j<=i;j++) { sprintf(binstr,"%s%c",binstr,(""!""<buf[j]&&buf[j]<=""~"")?buf[j]:"".""); } printf("%s\n",binstr); } else { sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]); } } if (0!=(i%16)) { k=16-(i%16); for (j=0;j<k;j++) { sprintf(binstr,"%s ",binstr); } sprintf(binstr,"%s ",binstr); k=16-k; for (j=i-k;j<i;j++) { sprintf(binstr,"%s%c",binstr,(""!""<buf[j]&&buf[j]<=""~"")?buf[j]:"".""); } printf("%s\n",binstr); } } |
3分 |
for循环打印
|
5分 |
使用1楼的代码,给个测试用例,你看看:
int main(void) { char buf[40] = "125a3ADC566742->->45!23afasdb2345~+asd"; HexDump(buf, strlen(buf), (int)buf); return 0; } //002ffdd4 - 31 32 35 61 33 41 44 43 35 36 36 37 34 32 2d 3e 125a3ADC566742-> //002ffde4 - 2d 3e 34 35 21 32 33 61 66 61 73 64 62 32 33 34 ->45!23afasdb234 //002ffdf4 - 35 7e 2b 61 73 64 5~+ asd |
2分 |
CString类的Format格式化输出
|