Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QByteArray toHex()
Qt 6.11 is out! See what's new in the release blog

QByteArray toHex()

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 5.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ckvsoft
    wrote on last edited by ckvsoft
    #1

    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;
    ba.append(str);
    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;

    Example;

    QByteArray ba;
    ba.append(str);
    QString result = QByteArray::fromBase64(ba);

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

    the Hex is "6a3e7c47efbfbdefbfbd3eefbfbd" (false one)

    What is wrong?

    Thanx for help Chris

    raven-worxR 1 Reply Last reply
    0
    • C ckvsoft

      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;
      ba.append(str);
      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;

      Example;

      QByteArray ba;
      ba.append(str);
      QString result = QByteArray::fromBase64(ba);

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

      the Hex is "6a3e7c47efbfbdefbfbd3eefbfbd" (false one)

      What is wrong?

      Thanx for help Chris

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @ckvsoft
      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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      C 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @ckvsoft
        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.

        C Offline
        C Offline
        ckvsoft
        wrote on last edited by
        #3

        @raven-worx
        Hi

        i have a string wich is an base64Encoded String

        QString encoded = "aj58R5u6PsY=";
        QByteArray ba;
        ba.append(encoded);
        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;
        ba.append(encoded);

        QString decoded = QByteArray::fromBase64(ba);

        // than in the next step

        QByteArray baDecoded;
        ba.append(decoded);

        QString decodedHEX = ba.toHex();

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

        Do you now what i meen?

        lg Chris

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 1 Reply Last reply
          0
          • SGaistS SGaist

            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.

            C Offline
            C Offline
            ckvsoft
            wrote on last edited by
            #5

            @SGaist

            Hi

            Thanx for welcome and the Hint.

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

            lg Chris

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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 :)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved