QByteArray

存储的是字节,二进制形式,即ascii码的二进制编码。输出的时候,会输出二进制对应的字符

即一个映射:
二进制到ascii码的映射

而.tohex()会将二进制转化为16进制字符,这里的16进制字符又是作为值域了,实际存储二进制编码已经变了

QByteArray array("abcdefghijklmn");
        QByteArray b=array.toHex();
        qDebug()<<array;
        qDebug()<<b;
        qDebug()<<b[0]<<b[1]<<b[2]<<b[3]-'0';

结果:

"abcdefghijklmn" 
"6162636465666768696a6b6c6d6e" 
6 1 6 2 //前3个是字符,最后一个是数字,说明,底层存的是16进制字符对应的二进制编码

例如:

QByteArray array("abc");
        qDebug()<<array;
        qDebug()<<array.toHex();

输出:

"abc" 
"616263" //a的16进制编码就是61

QString

string中保存的每个单位是char类型
str[i]到str[i].toLatin1()的区别就是Qchar和Byte(char)的区别
即从unicode16位编码(QChar)转化为ascii8位编码(char)
做这个转化是因为串口的write函数放送的只能是Byte(char)型数据

hstr=str[i].toAscii();//qt4

hstr=str[i].toLatin1();//qt5

测试代码:

QString str("love");
        qDebug()<<str[0]<<str[1]<<str[2].toLatin1();

结果:

'l' 'o' v //这里的v仍然是一个字符,只不过是Byte型

QChar

QString 的str[i]类型为QChar,大小为2字节,保存的是16位的unicode编码

The QChar class provides a 16-bit Unicode character.

str[i]返回的是一个QChar字符的应用,大小为2字节

QCharRef QString::operator[](int position)
Returns the character at the specified position in the string as a modifiable reference.

Example:

QString str;

if (str[0] == QChar('?'))
    str[0] = QChar('_');
The return value is of type QCharRef, a helper class for QString. When you get an object of type QCharRef, you can use it as if it were a QChar &. If you assign to it, the assignment will apply to the character in the QString from which you got the reference.

串口收发数据的16进制的含义

可能不同的串口软件有不同的含义

比如下面这个软件,16进制发送就只是在发送端的显示层将字符串转换为16进制,发的是同一个东西,16进制接收就是在接收端显示层将字符串转换为16进制字符串。

bytes图像 bytes to_字符串

对于另一个串口助手来说

1.发数据

正常发送就是发送输入的字符串,
16进制发送就是在发送前在发送端将原字符串转换为16进制字符串发出去
16进制发送是以发送字符串作为16进制编码,而不是发字符串的16进制编码。(就是说我发送框输入的当成是16进制编码,发送时(每两个字符当成一个字节)转换为对应的字节流)

2.收数据

正常接收就是将收到的字节流转为字符串显示出来
16进制接收就是在接收端先将收到的字节流转换为16进制只对应的字节流再转换为字符串显示出来

总结

一、收
收正常:字节流对应的字符串
收16进制:字节流的16进制对应字符串

二、发
发正常:字符串对应的字节流
发16进制:字符串作为16进制对应的字节流

bytes图像 bytes to_16进制_02

bytes图像 bytes to_字符串_03