Int16 to QByteArray
-
wrote on 29 Nov 2016, 14:24 last edited by
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
-
wrote on 29 Nov 2016, 14:38 last edited by
Hi,
Your code is correct if you run it on a normal PC under, say, Windows 10. I tried it and got: "\x00\x00!\x13" (! is 33 which is 0x21). On which OS do you run the code? Is it maybe a big endian machine?
-Michael. -
Hi,
Out of curiosity, why not use QByteArray::number ?
-
wrote on 29 Nov 2016, 14:49 last edited by m.sue
Hi,
withqDebug() << QByteArray::number(id,16);
you will get the four characters "2113". So it depends on what you really need.
-Michael. -
wrote on 29 Nov 2016, 15:06 last edited by
@m-sue
I also got \x00\x00!\x13 on Windows, but didn't knew that '!' is the same as 0x21..Is there a way to rewrite it as 0x21 ?
Thanks!
-
Hi,
withqDebug() << QByteArray::number(id,16);
you will get the four characters "2113". So it depends on what you really need.
-Michael.wrote on 29 Nov 2016, 15:14 last edited by@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. -
@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.Use
QDataStream
over the bytearray.qint32 whatever = 5; QByteArray data; QDataStream out(&data, QIODevice::WriteOnly); // By default is in big endian out << whatever;
-
Use
QDataStream
over the bytearray.qint32 whatever = 5; QByteArray data; QDataStream out(&data, QIODevice::WriteOnly); // By default is in big endian out << whatever;
wrote on 29 Nov 2016, 18:13 last edited by@kshegunov Thanks, less code this way. But still x00!\x13 and not 0x00\0x21\0x13.
Is there a way to rewrite it to regular hex codes ? Or does every controller understand this notation ?
-
Hi
Will the micro controller also run Qt and be able to use QDataStream ? -
@kshegunov Thanks, less code this way. But still x00!\x13 and not 0x00\0x21\0x13.
Is there a way to rewrite it to regular hex codes ? Or does every controller understand this notation ?
!
and\0x21
are one and the same binary-wise, I don't understand the question, rewrite it to what? It already is\0x21
. -
@m-sue
I also got \x00\x00!\x13 on Windows, but didn't knew that '!' is the same as 0x21..Is there a way to rewrite it as 0x21 ?
Thanks!
@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
1/11