Converting a QByteArray in Hexa format into an Int
-
Hello,
How can I convert a QByteArray of 4 bytes to an integer?Example:
QByteArray mydata = QByteArray::fromHex("00004A9E");
int mydataInteger = ???In this example, I would like the integer value should be equal to 19102.
Thanks you.
@Pantoufle https://doc.qt.io/qt-6/qbytearray.html#toInt
with base = 16 -
@Pantoufle https://doc.qt.io/qt-6/qbytearray.html#toInt
with base = 16@jsulm Doest seems to work, I tried with this code :
ByteArray mydata = QByteArray::fromHex("00004A9E"); bool ok; int a = mydata.toInt(&ok, 16); if(!ok) std::cout << "Fail convert" << std::endl; std::cout << "A TEST : " << a << std::endl;
This code return a converting error
-
@jsulm Doest seems to work, I tried with this code :
ByteArray mydata = QByteArray::fromHex("00004A9E"); bool ok; int a = mydata.toInt(&ok, 16); if(!ok) std::cout << "Fail convert" << std::endl; std::cout << "A TEST : " << a << std::endl;
This code return a converting error
@Pantoufle You do not need fromHex...
-
@Pantoufle You do not need fromHex...
Sorry, but this may lack some context. Actually, I don't really construct this QByteArray using "fromHex"; instead, I receive it through a serial connection and then store it in a QByteArray.
In the example above, indeed, if I remove the "fromHex" part, it works, but then the QByteArray will be 8 bytes long, which is not my case. The data will be only 4 bytes, which corresponds to creating a QByteArray with "fromHex".
-
Sorry, but this may lack some context. Actually, I don't really construct this QByteArray using "fromHex"; instead, I receive it through a serial connection and then store it in a QByteArray.
In the example above, indeed, if I remove the "fromHex" part, it works, but then the QByteArray will be 8 bytes long, which is not my case. The data will be only 4 bytes, which corresponds to creating a QByteArray with "fromHex".
@Pantoufle said in Converting a QByteArray in Hexa format into an Int:
but then the QByteArray will be 8 bytes long,
Wrong, two hex digits = 1 byte.
-
Sorry, but this may lack some context. Actually, I don't really construct this QByteArray using "fromHex"; instead, I receive it through a serial connection and then store it in a QByteArray.
In the example above, indeed, if I remove the "fromHex" part, it works, but then the QByteArray will be 8 bytes long, which is not my case. The data will be only 4 bytes, which corresponds to creating a QByteArray with "fromHex".
@Pantoufle Sorry, I don't understand your problem. As @mpergand wrote two hex digids represent one byte. Did you actually try toInt? And are you sure that what you receive is actually hex encoded number-string? How exactly are those numbers sent?
-
Sorry, but this may lack some context. Actually, I don't really construct this QByteArray using "fromHex"; instead, I receive it through a serial connection and then store it in a QByteArray.
In the example above, indeed, if I remove the "fromHex" part, it works, but then the QByteArray will be 8 bytes long, which is not my case. The data will be only 4 bytes, which corresponds to creating a QByteArray with "fromHex".
@Pantoufle
Decide whether your input is:- 4 bytes of (binary) data, i.e. a binary representation of ant
int
, whether is low- or high-endian order; or - 8 hexadecimal characters to be converted to an
int
(presumably high-to-low).
Then you/we will know what you're actually asking. Till then it is quite unclear, and only you know what your input is....
- 4 bytes of (binary) data, i.e. a binary representation of ant
-
@Pantoufle said in Converting a QByteArray in Hexa format into an Int:
but then the QByteArray will be 8 bytes long,
Wrong, two hex digits = 1 byte.
I know that two hex digits = 1 byte, therefore in my example if i remove the fromHex it create a 8 bytes QbyteArray :
QByteArray mydata ="00004A9E"; std::cout << "My data size : " << mydata.size() << std::endl;
Output:
My data size : 8
Maybe i miss understood the "You don't need the fromHex" quote and I'm sorry if so.
It's a 4 bytes if binary data representation of an int, high endian
-
I've found an answer that works :
QByteArray mydata = QByteArray::fromHex("00004A9E"); QDataStream ds(mydata); ds.setByteOrder(QDataStream::BigEndian); int mydataInteger; ds >> mydataInteger; std::cout << "Int content : " << mydataInteger << std::endl;
But I would be glad if you can make me understand why toInt() don't work.
-
I've found an answer that works :
QByteArray mydata = QByteArray::fromHex("00004A9E"); QDataStream ds(mydata); ds.setByteOrder(QDataStream::BigEndian); int mydataInteger; ds >> mydataInteger; std::cout << "Int content : " << mydataInteger << std::endl;
But I would be glad if you can make me understand why toInt() don't work.
@Pantoufle said in Converting a QByteArray in Hexa format into an Int:
why toInt() don't work
This code prints 19102:
QByteArray n("00004A9E"); bool ok; qDebug() << n.toInt(&ok, 16);
-
@Pantoufle said in Converting a QByteArray in Hexa format into an Int:
why toInt() don't work
This code prints 19102:
QByteArray n("00004A9E"); bool ok; qDebug() << n.toInt(&ok, 16);
Yes but the code u just send is a 8 bytes QbyteArray :
QByteArray mydata("00004A9E"); std::cout << "My data : " << mydata.size() << std::endl; bool ok; int a = mydata.toInt(&ok, 16); if(!ok) std::cout << "Fail convert" << std::endl; std::cout << "My data int " << a << std::endl;
-
Yes but the code u just send is a 8 bytes QbyteArray :
QByteArray mydata("00004A9E"); std::cout << "My data : " << mydata.size() << std::endl; bool ok; int a = mydata.toInt(&ok, 16); if(!ok) std::cout << "Fail convert" << std::endl; std::cout << "My data int " << a << std::endl;
-
@Pantoufle
What is the format of the original data ?
ASCII in hex format ?
Binary in big endian ?
Other ? -
Yes but the code u just send is a 8 bytes QbyteArray :
QByteArray mydata("00004A9E"); std::cout << "My data : " << mydata.size() << std::endl; bool ok; int a = mydata.toInt(&ok, 16); if(!ok) std::cout << "Fail convert" << std::endl; std::cout << "My data int " << a << std::endl;
@Pantoufle this is probably through the ambiguity of the QByteArray constructor, wich builds down to char* or an other QByteArray
use the proper constructor from a String, and it should be fine:
auto mydata = QString("00004A9E").toLatin1(); std::cout << "My data : " << mydata.size() << std::endl; bool ok; int a = mydata.toInt(&ok, 16); if(!ok) std::cout << "Fail convert" << std::endl; std::cout << "My data int " << a << std::endl;
-
@Pantoufle
What is the format of the original data ?
ASCII in hex format ?
Binary in big endian ?
Other ?@mpergand said in Converting a QByteArray in Hexa format into an Int:
@Pantoufle
What is the format of the original data ?
ASCII in hex format ?
Binary in big endian ?
Other ?Binary in big endian
@J-Hilk It works but same problem as before, the QByteArray generated by the line (auto mydata = QString("00004A9E").toLatin1();) is 8 bytes long.
-
@mpergand said in Converting a QByteArray in Hexa format into an Int:
@Pantoufle
What is the format of the original data ?
ASCII in hex format ?
Binary in big endian ?
Other ?Binary in big endian
@J-Hilk It works but same problem as before, the QByteArray generated by the line (auto mydata = QString("00004A9E").toLatin1();) is 8 bytes long.
@Pantoufle said in Converting a QByteArray in Hexa format into an Int:
the QByteArray generated by the line (auto mydata = QString("00004A9E").toLatin1();) is 8 bytes long.
But that's correct - you pass 8 bytes to a QString ctor and convert it back to a QByteArray.
-
@mpergand said in Converting a QByteArray in Hexa format into an Int:
@Pantoufle
What is the format of the original data ?
ASCII in hex format ?
Binary in big endian ?
Other ?Binary in big endian
@J-Hilk It works but same problem as before, the QByteArray generated by the line (auto mydata = QString("00004A9E").toLatin1();) is 8 bytes long.
@Pantoufle said in Converting a QByteArray in Hexa format into an Int:
Binary in big endian
Then why do you pass a string to QByteArray?
You should rather use https://doc.qt.io/qt-6/qbytearray.html#fromRawData
Also see https://doc.qt.io/qt-6/qtendian.html -
@Pantoufle said in Converting a QByteArray in Hexa format into an Int:
the QByteArray generated by the line (auto mydata = QString("00004A9E").toLatin1();) is 8 bytes long.
But that's correct - you pass 8 bytes to a QString ctor and convert it back to a QByteArray.
My case is equivalent to :
QByteArray mydata = QByteArray::fromHex("0000049E");
Which generate a 4 bytes array, so yeah it's normal that it generates a 8 bytes in the previous example but just it's not what I want.
QByteArray mydata = QByteArray::fromHex("0000049E");
Represent a binary in big endian, 4 bytes long.
I didn't know about QtEndian and fromRawData, I will check that thx.
Also I found that it works with QByteArrayLiteral("\x00\x00\x04\x9E") -
My case is equivalent to :
QByteArray mydata = QByteArray::fromHex("0000049E");
Which generate a 4 bytes array, so yeah it's normal that it generates a 8 bytes in the previous example but just it's not what I want.
QByteArray mydata = QByteArray::fromHex("0000049E");
Represent a binary in big endian, 4 bytes long.
I didn't know about QtEndian and fromRawData, I will check that thx.
Also I found that it works with QByteArrayLiteral("\x00\x00\x04\x9E")@Pantoufle
Hi. I don't understand if you still have a problem or a question? You want a 4 byte integer, and you have one. I don't understand what the relevance is of a string you choose to pass toQByteArray::fromHex()
as a convenient way of getting the 4 bytes into memory, who cares what the string is or how long it is? It has nothing to do with how your device works, which does not use hex or strings, it just sends a 4-byte integer.Which should be convertible viaQByteArray::toInt()
provided the byte order matches your machine's endian order (else it would need swapping).My bad, see @Christian-Ehrlicher's clarification of
QByteArray::toInt()
below!