boost::scoped_array<int> buf(new int[51]) //其实就是定义了 int数组
打印结果:
XX8F80-
boost::scoped_array<char> buf(new char[51]) //其实就是定义了 char数组
打印结果:
FFFFFF80-
预期收到/串口软件收到:
80 8F (Hex方式显示) //其实就是2个数据
//打印
for(int i=0; i<50; i++)
{
printf(“%02X-“, buf[i]);
}
本人应该怎么定义接收数组或怎么转换已接受到的数据,才能使之像串口软件一样显示同样的数据呢?
解决方案
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); } }