qint32 values into QByteArray
Solved
General and Desktop
-
Hello,
Im running this code with Desktop Qt 5.15.2 GCC 64 bit.
#include <QCoreApplication> #include <QByteArray> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); quint32 value = 0x11223344; QByteArray payload; payload.resize(4); qInfo() << "Payload size: " << payload.size(); payload[0] = static_cast<quint8>((value & 0xFF000000) >> 24); payload[1] = static_cast<quint8>((value & 0x00FF0000) >> 16); payload[2] = static_cast<quint8>((value & 0x0000FF00) >> 8); payload[3] = static_cast<quint8>((value & 0x000000FF) ); qInfo() << "Output: " << payload; qInfo() << "Expected hex: " << hex \ << static_cast<quint8>((value & 0xFF000000) >> 24) \ << static_cast<quint8>((value & 0x00FF0000) >> 16) \ << static_cast<quint8>((value & 0x0000FF00) >> 8) \ << static_cast<quint8>((value & 0x000000FF) ); return a.exec(); }
Output:
Payload size: 4 Output: "\x11\"3D" Expected hex: 11 22 33 44
Why output and expected are not equal? How can I do to make output and expected equal?
-
@Drazz said in qint32 values into QByteArray:
Why output and expected are not equal? How can I do to make output and expected equal?
representation issues of qDebug() and QByteArray.
use
qInfo() << "Output: " << payload.toHex(' ');
btw you should use QDataStream
-
Further to @J-Hilk's response, the four bytes in the array map to the string you see:
Output: \x11 \" 3 D Expected hex: 11 22 33 44
The backslash before the " is there because the output is designed to be a usable C++ string and you escape embedded quotes.