简体   繁体   English

Qt QByteArray无符号数据类型

[英]Qt QByteArray unsigned datatype

I need to send data over rs232,but I'm facing a problem. 我需要通过rs232发送数据,但是我遇到了问题。
When I send byte representing integer over 128, it looks like QByteArray changes the content I feed it: 当我发送表示大于128的整数的字节时,看起来QByteArray更改了我提供的内容:

uchar uc_array[]={0x41,0xAA}; //65 170
QByteArray qb_array = QByteArray();
qb_array.append(uc_array[0]);
qb_array.append(uc_array[1]);
cout<<(uint)qb_array[0]<<endl //65
cout<<(uint)qb_array[1]<<endl //4294967210

Why 为什么

 cout<<(uint)qb_array[1]<<endl

doesn't print 170, but 4294967210 instead ? 不打印170,而是打印4294967210?

Here is my explanation of why you print 4294967210 instead of 170, which is your original question: 这是我对为什么打印4294967210而不是170的解释,这是您的原始问题:

  1. qb_array[1] returns a signed char type with 0xAA value qb_array[1]返回值为0xAA的带signed char类型
  2. When you cast a signed char to uint type: 将带signed charuint输入:
    • the value is first promoted to int type, keeping the sign: it becomes 0xFFFFFFAA 首先将值提升为int类型,并保留符号:它变为0xFFFFFFAA
    • then it is interpreted as unsigned, so 4294967210 in decimal format. 那么它将被解释为无符号,因此为4294967210(十进制格式)。
  3. Printing this value in decimal format gives 4294967210. 以十进制格式打印此值得到4294967210。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM