Solved QByteArray problem
-
@VRonin So it is ok with storing 0xFF value , if i need this value in my code from QByteArray it would be given ?
-
Yes, the if you use
[]
that casts it automatiocally to signed but the value is still0xff
-
-
@JonB
Wow, that totally slipped my mind, 👍 -
@JonB said in QByteArray problem:
@Zizione
In addition to the solutions offered by @VRonin....I am not a C++-er, but I believe in your case your can use the
U
suffix to make your constant valuesunsigned int
and thus convertible tounsigned char
:ba[0] = 0xFFU;
?
Yeah, but
ba
issigned char
... -
@aha_1980
true, but the warning one gets withba[0] = 0xFF
is not about usigned and signed char but rather betweenint
andchar
0xFFU
==unsigned char(0xFF)
==uchar(0xFF)
!=0xFF
the implicit conversation to ba[0] does not "drop" information
-
@J.Hilk have you tried?
QByteArray ba; ba.resize(4); ba[0] = char(0xFF); // no warning ba[1] = 0xFFU; // warning: implicit conversion changes signedness: 'unsigned int' to 'char' ba[2] = 0xFF; // warning: implicit conversion changes signedness: 'int' to 'char'
Edit: screenshot
-
@aha_1980 said in QByteArray problem:
@J.Hilk have you tried?
I always do, well 95%, before I post ;-)
to be honest, I'm surprised about the uint part.
-
then you can be surprised about the uchar() part too.
But the biggest surprise: your code will not even compile in gcc and clang because of line 65.
And one more hint: your code will crash as you access invalid index in ba ;)
-
@aha_1980 said in QByteArray problem:
@J.Hilk
But the biggest surprise: your code will not even compile in gcc and clang because of line 65.I saw you using linux in the screen shot and testet that there than, and noticed it too
And one more hint: your code will crash as you access invalid index in ba ;)
nope doesn't crash, ignore the project name its my test anything project x).
!resize is apparently not a necessity?
Edit:
also ignore value 0, I commeted that line out, to run it with gcc, so theres a random value.
// ba[0] = unsigned char(0xFF);
-
@J.Hilk said in QByteArray problem:
resize is apparently not a necessity?
Ah, my fault: "If an assignment is made beyond the end of the byte array, the array is extended with resize() before the assignment takes place."
A big difference to QVector or QList here.