Antonio Del Sannio
Antonio Del Sannio

Reputation: 315

Qt QByteArray unsigned datatype

I need to send data over rs232,but I'm facing a problem.
When I send byte representing integer over 128, it looks like QByteArray changes the content I feed it:

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 ?

Upvotes: 0

Views: 307

Answers (1)

Laurent H.
Laurent H.

Reputation: 6526

Here is my explanation of why you print 4294967210 instead of 170, which is your original question:

  1. qb_array[1] returns a signed char type with 0xAA value
  2. When you cast a signed char to uint type:
    • the value is first promoted to int type, keeping the sign: it becomes 0xFFFFFFAA
    • then it is interpreted as unsigned, so 4294967210 in decimal format.
  3. Printing this value in decimal format gives 4294967210.

Upvotes: 3

Related Questions