char数组中的0x00看不到的问题

  • Post author:
  • Post category:其他




网络调试助手发来的数据:

7E 24 00 00 01 01 02 03 04 05 7E 03 7D 09 52 01 00 00 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03



QT收到的数据:

QByteArray tempArray: “

~$\x00\x00\x01\x01\x02\x03\x04\x05~\x03}\tR\x01\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03



在这里插入图片描述

当我把接收到的 ~ $ \x00 三个字节的数据拷贝新建的数组frame_head中时,

char frame_head[3] = {0};
memcpy(&frame_head,tempArray.data(),sizeof(frame_head));

之后打印结果:



1
qDebug() <<"得到的结果"<<  frame_head[0] << frame_head[1] << frame_head[2];
// 结果:126 36 
无法得到frame_head[2]的数据。


2
qDebug() <<"得到的结果"<<  frame_head[0] << frame_head[1] << frame_head[2] <<"abc";
// 结果:126 36 
无法得到frame_head[2]和"abc"。这样一来abc也无法得到了。


3
qDebug() <<"得到的结果"<<  frame_head[0] << frame_head[1] << frame_head[2] <<"abc";
qDebug() << "efg"
// 结果:126 36 
		 	 "efg"
这样可以得到下面打印的数据"efg"。


4
但是,当把frame_head定义为uchar类型时,可以得到结果:126 36 0

这是为什么呢?

找了一下

问题原因

,但不知道是不是这个问题的解释。

里面说char数组中的0x00就是’\0’了,所以打印之后是看不到的。

哪位有缘的大佬看到给个答案呗~



版权声明:本文为weixin_44486830原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。