Search code examples
c++qtqbytearray

QByteArray conversion from text


I have a textedit where I key in the hex number, and then this text to be converted into QByteArray.

This is my code:

QByteArray parsedValue = QByteArray::fromHex(expectedPacketStr.toUtf8());
qDebug() << parsedValue;

when I set it to 001102, then console log reports "\x00\x11\x02" which is what I expected.

But if i set it to 001122, the console logs reports "\x00\x11\"" which is missing the x22 byte.

I really cannot understand what's going on. Anybody have any clue why is this so???


Solution

  • 0x22 is the character " in ascii, so it's just qDebug() that is interpreting it, and nothing is missing inside QByteArray.

    To convince you, you can always display the array one by one:

    for (auto b : parsedValue)
        qDebug() << (int)b;