QByteArray range issue?
-
Dear all,
i have:
void LED::setColor(int red, int green, int blue) { QByteArray writeData; writeData.resize(15); // red int red_checksum = 256 - 5 - 97 - 16 - red; if (red_checksum < 0) { red_checksum = red_checksum + 256; } writeData[0] = 5; writeData[1] = 97; writeData[2] = 16; writeData[3] = red; writeData[4] = red_checksum; // green int green_checksum = 256 - 5 - 97 - 17 - green; if (green_checksum < 0) { green_checksum = green_checksum + 256; } writeData[5] = 5; writeData[6] = 97; writeData[7] = 17; writeData[8] = green; writeData[9] = green_checksum; // blue int blue_checksum = 256 - 5 - 97 - 18 - blue; if (blue_checksum < 0) { blue_checksum = blue_checksum + 256; } writeData[10] = 5; writeData[11] = 97; writeData[12] = 18; writeData[13] = blue; writeData[14] = blue_checksum; qDebug() << "Red" << QByteArray::number(writeData[0])<< QByteArray::number(writeData[1]) << QByteArray::number(writeData[2]) << QByteArray::number(writeData[3]) << QByteArray::number(writeData[4]); qDebug() << "Blue" << QByteArray::number(writeData[5]) << QByteArray::number(writeData[6]) << QByteArray::number(writeData[7]) << QByteArray::number(writeData[8]) << QByteArray::number(writeData[9]); qDebug() << "Green" << QByteArray::number(writeData[10]) << QByteArray::number(writeData[11]) << QByteArray::number(writeData[12]) << QByteArray::number(writeData[13]) << QByteArray::number(writeData[14]); }
and when calling setColor(255, 0, 0), the outcome of writeData[3] as -1 instead of 255.
Also, same for calculation of red_checksum, green_checksum and blue_checksum,
the result is:red_checksum as -117 green_checksum as -119 blue_checksum as -120 code_text
but realy it should be:
red_checksum = 256 - 5 - 97 - 16 - red = 256 - 5 - 97 - 16 - 255 = -117; if(red_checksum<0) {red_checksum=red_checksum+256} which means red_checksum= -117 + 256 = 139
same for other colors...
so is this range issue? is QByteArray capable only of 254chars or what is the problem here?
-
@shokarta said in QByteArray range issue?:
the outcome of writeData[3] as -1 instead of 255
-1 and 255 have exact same binary representation:
255: b11111111
-1 (two complement): b00000001 -> (invert bits) b11111110 -> (add 1) b11111111
See https://en.wikipedia.org/wiki/Two's_complement
You need to interpret writeData[3] as unsigned char to see 255. -
Thanks, but i guess the Binary is not the same for -117 and 139.
Basicaly I need to write the symbol which is behind DEC number…
so from ASCII table i need to insert the char from column "Symbol" which is behind the DEC number.
And it needs to be inserted into QByteArray…
Can I kindly ask you to show me an exmaple how? -
@shokarta There is https://doc.qt.io/qt-5/qchar.html#fromLatin1 and https://doc.qt.io/qt-5/qchar.html#toLatin1 to convert from ASCII
-
@JonB said in QByteArray range issue?:
@shokarta said in QByteArray range issue?:
Thanks, but i guess the Binary is not the same for -117 and 139.
? 117 + 139 == 256, so they are the same.
no,
im assigning via writeData[3] = 255;
then trying to see what is behind by:
qDebug() << QByteArray::number(writeData[3]) << static_cast<char>(writeData[3]);
so instead of having:
255 ÿ
im having:
-1 ?and same goes for the:
red_checksum = 256 - 5 - 97 - 16 - red //256 - 5 - 97 - 16 - 255 = -117
if(red_checksum<0) {red_checksum=red_checksum+256} //which means red_checksum= -117 + 256 = 139
writeData[4] = red_checksum;
and then printing:
qDebug() << QByteArray::number(writeData[4]) << static_cast<char>(writeData[4]);
I expect to have:
139 ‹
but im having:
-117 y -
@shokarta No.
@JonB is absolutely correct.If you want to check against the upper range of unsigned, cast it properly
qDebug() << QByteArray::number(writeData[3]) << static_cast<char>(writeData[3]);qDebug() << QByteArray::number(writeData[3]) << static_cast<unsigned char>(writeData[3]);qDebug () << static_cast <unsigned char> (-117) << static_cast <unsigned char> (139) << ( static_cast <unsigned char> (-117) == static_cast <unsigned char> (139));
-> 139 139 true
-
QByteArray seems to be just an array of char, which are signed values.
In other words, the range is from -128 to 127.
In binary format, the last bit is for the sign. 0 is for positive values and 1 is for negative values.
Numbers from 0 to 127 are mapped to 0000 0000 to 0111 1111.
0111 1111 = 127 <==== notice the last bit is 0. 127 is a positive value.
Numbers from -128 to -1 are mapped to 1000 0000 to 1111 1111.127+1 would cause an overflow. You get 1000 0000 which is actually -128.
It sounds like you want to work in the range of -255 to 255 or maybe a wider range.
In that case, you would have to use a short (16 bit signed) or int (32 bit signed) array.
I don't know if Qt has such a thing.
You can use STL arrays or write your own classes. -
@stretchthebits said in QByteArray range issue?:
It sounds like you want to work in the range of -255 to 255
No, he simply don't understand the difference between a signed and an unsigned char data type.
-
This post is deleted!
-
@Christian-Ehrlicher
That's ok, that is why I explained to him how it works for the case of char. It is up to him if he wants short or int. He seems to be working with colors and using 8 bit but char is not appropriate for what he is doing.