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::fromHex("a").toHex() == QByteArray("a")) false?!
QtWS25 Last Chance

(QByteArray::fromHex("a").toHex() == QByteArray("a")) false?!

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 7.3k Views
  • 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.
  • M Offline
    M Offline
    MichelS
    wrote on last edited by
    #1

    Would you expect the above to be true?

    I tried the following and the results are puzzling me.

    @
    qDebug() << (QByteArray::fromHex("a").toHex() == QByteArray("a")); // false
    qDebug() << QByteArray::fromHex("a").toHex(); // 0a
    qDebug() << QByteArray::fromHex("aa").toHex(); // aa
    qDebug() << QByteArray::fromHex("aaa").toHex(); // 0aaa
    @

    I'm a bit confused... What do you think?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Looks ok to me.

      When you call toHex(), QBA translates the symbol into a hex value (1 byte, hence the '0a' you get in line 2 and 4).

      Line 1 is fine because you are comparing a value converted into hex with a string "a". Remember: "a" != 'a' != 0xa

      (Z(:^

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MichelS
        wrote on last edited by
        #3

        So how do I convert a hex string into Hex BA without the leading zero?

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

          Hi,

          Did you try "fromHex":http://qt-project.org/doc/qt-4.8/qbytearray.html#fromHex ?

          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
          • V Offline
            V Offline
            volnei
            wrote on last edited by
            #5

            Hi,

            QByteArray ba = "BA";
            qDebug() << ba.toHex();

            "4241"

            Is this you want?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MichelS
              wrote on last edited by
              #6

              fromHex/toHex works quite well with a even number of hex-digits.
              But with odd numbers there's always the leading zero.

              I think I understand the problem now.
              One character takes only half a byte. So if there is an odd number of characters there must be 4 bits padded. These are the first 4 bits.

              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