Nominate our 2022 Qt Champions!

QByteArray toHex()

  • Hallo

    I hope this is the right Forum.

    I have troubles with display a QString in Hex.

    When i do:

    QByteArray ba;
    ba.append(reinterpret_cast<const char *>(out), sizeof(out));
    QString resultBase63 = ba.toBase64();

    i Got a String , i this Case "aj58R5u6PsY="

    When i do a decode

    QByteArray ba;
    QString result = QByteArray::fromBase64(ba).toHex();

    i got a Hex with "6a3e7c479bba3ec6"

    But when i have the encoded String the result is an other false one;


    QByteArray ba;
    QString result = QByteArray::fromBase64(ba);

    QByteArray baResult;
    QString resultHex = baResult.toHex();

    the Hex is "6a3e7c47efbfbdefbfbd3eefbfbd" (false one)

    What is wrong?

    Thanx for help Chris

  • Moderators

    sry, for me it's not clear what you want. Why is the result false?
    Also it's not clear how the data you pass to append() looks like.

  • @raven-worx

    i have a string wich is an base64Encoded String

    QString encoded = "aj58R5u6PsY=";
    QByteArray ba;
    QString decoded = QByteArray::fromBase64(ba).toHex();

    // Here is the Right result
    // decoded HEX = 6a3e7c479bba3ec6

    But when i do this not in one run

    QString encoded = "aj58R5u6PsY=";
    QByteArray ba;

    QString decoded = QByteArray::fromBase64(ba);

    // than in the next step

    QByteArray baDecoded;

    QString decodedHEX = ba.toHex();

    // decoded HEX is "6a3e7c47efbfbdefbfbd3eefbfbd" and NOT "6a3e7c479bba3ec6"

    Do you now what i meen?

    lg Chris

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Because you are not consistent in your code.

    In the one-liner you are working only with QByteArrays: fromBase64 returns a QByteArray on which you call toHex() which also returns a QByteArray that is then converted into a QString.

    On the second version, you put a QByteArray in a QString that you then put back in a QByteArray that you again put in a QString. QString works internally on UTF16 data while QByteArray works on bytes. Don't mix them, work only with QByteArrays in this case.

  • @SGaist


    Thanx for welcome and the Hint.

    I rewrite some of my function to return QByteArray. Now it works correctly.

    lg Chris

  • Lifetime Qt Champion

    You're welcome !

    Since you have it working now, please mark the thread as solved using the "Topic Tool" button so that other forum users may know a solution has been found :)

    Also, while browsing the forum, consider up-voting answers that helped you. It will make them easier to find for other users :)

Log in to reply