Converting a QByteArray in Hexa format into an Int
-
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! -
@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!@JonB said in Converting a QByteArray in Hexa format into an Int:
Which should be convertible via QByteArray::toInt() provided the byte order matches your machine's endian order (else it would need swapping).
No, you're wrong here.
QByteArray::toInt() converts a string representation of a number to an integer.int32_t val = *(reinterpret_cast<int32_t*>(ba.constData()))
if the endian is the same on the source and destination, otherwise use the qEndian helper functions.
-
@JonB said in Converting a QByteArray in Hexa format into an Int:
Which should be convertible via QByteArray::toInt() provided the byte order matches your machine's endian order (else it would need swapping).
No, you're wrong here.
QByteArray::toInt() converts a string representation of a number to an integer.int32_t val = *(reinterpret_cast<int32_t*>(ba.constData()))
if the endian is the same on the source and destination, otherwise use the qEndian helper functions.
@Christian-Ehrlicher
Oh, I am so wrong, I should have looked atQByteArray::toInt()
docs before answering instead of assuming it was a binary operation! I now understand where the confusion lies totally! I will cross out my earlier. -
@JonB said in Converting a QByteArray in Hexa format into an Int:
Which should be convertible via QByteArray::toInt() provided the byte order matches your machine's endian order (else it would need swapping).
No, you're wrong here.
QByteArray::toInt() converts a string representation of a number to an integer.int32_t val = *(reinterpret_cast<int32_t*>(ba.constData()))
if the endian is the same on the source and destination, otherwise use the qEndian helper functions.
Just use the Qt endian functions every time, since they evaluate to a no-op (ie
static_cast
) when the source and destination match anyway. Eg:const QByteArray mydata = QByteArray::fromHex("00004A9E"); const quint32 mydataInteger = qFromBigEndian<quint32>(mydata); qDebug() << mydata; qDebug() << mydataInteger;
Outputs:
"\x00\x00J\x9E" 19102
Cheers.