Int16 to QByteArray
-
Hello Everyone,
I am trying to convert a int into 4 bytes, using the following code:
unsigned int id = 0x2113; // = 8467 QByteArray name; name.append((id >> 24) & 0XFF); name.append((id >> 16) & 0XFF); name.append((id >> 8) & 0XFF); name.append(id & 0XFF); qDebug() << name;
And it works until id is bigger than 0xFF, then the result is like \x00\x00\bA and it has to be \x00\x00\x21\x13
What is going wrong?
Thanks
-
Hi,
Out of curiosity, why not use QByteArray::number ?
-
@m.sue said in Int16 to QByteArray:
Hi,
withqDebug() << QByteArray::number(id,16);
you will get the four characters "2113". So it depends on what you really need.
-Michael.@SGaist
Yes I've tried that function but I'll send the byteArray with TCP to a micro controller after the transformation, so QByteArray::nummer or .setNum is not an option. -
Hi
Will the micro controller also run Qt and be able to use QDataStream ? -
@TomHoe said in Int16 to QByteArray:
didn't knew that '!' is the same as 0x21..
See http://www.ascii-code.com/
'!'
== 33 == 0x21 == 0b00100001@TomHoe said in Int16 to QByteArray:
Is there a way to rewrite it to regular hex codes ? Or does every controller understand this notation ?
Again,
'!'
== 33 == 0x21 == 0b00100001Those are different ways of displaying the same bytes. The controller does not see any "hex" or "notation"; it only sees the sequence of 0's and 1's.
I recommend you learn about how bits/bytes are stored in computer memory: http://statmath.wu.ac.at/courses/data-analysis/itdtHTML/node55.html