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(QString.toUtf8()) presenting undesired behavior
Forum Updated to NodeBB v4.3 + New Features

QByteArray::fromHex(QString.toUtf8()) presenting undesired behavior

Scheduled Pinned Locked Moved Solved General and Desktop
qbytearraytoutf8fromhex
15 Posts 4 Posters 1.8k 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.
  • Christian EhrlicherC Christian Ehrlicher

    @JonB said in QByteArray::fromHex(QString.toUtf8()) presenting undesired behavior:

    Slightly strange that causes it to shift at left rather than at right?

    The he decoding starts from the back. Don't know why though but the documentation is explicit: Input is not checked for validity

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #6

    @Christian-Ehrlicher said in QByteArray::fromHex(QString.toUtf8()) presenting undesired behavior:

    he decoding starts from the back.

    I sit and stare at

        hexstring = textStream.readLine();
        ba.append(QByteArray::fromHex(hexstring.toUtf8()));
    

    and just cannot see that!? :(

    Though anyway of course it doesn't matter, he needs to fix the character count.

    KroMignonK 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #7

      @JonB said in QByteArray::fromHex(QString.toUtf8()) presenting undesired behavior:

      and just cannot see that!? :(

      Forgot a Tat the start :D

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • JonBJ JonB

        @Christian-Ehrlicher said in QByteArray::fromHex(QString.toUtf8()) presenting undesired behavior:

        he decoding starts from the back.

        I sit and stare at

            hexstring = textStream.readLine();
            ba.append(QByteArray::fromHex(hexstring.toUtf8()));
        

        and just cannot see that!? :(

        Though anyway of course it doesn't matter, he needs to fix the character count.

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #8

        @JonB said in QByteArray::fromHex(QString.toUtf8()) presenting undesired behavior:

        I sit and stare at
        hexstring = textStream.readLine();
        ba.append(QByteArray::fromHex(hexstring.toUtf8()));

        and just cannot see that!? :(

        when reading from stream, you got an QByteArray.
        You have to transform it to QString to be able to use QByteArray::fromHex().

        I would to it with hexstring.toLatin1() but it doesn't really matter, because there are only '0'-'9' or 'A'-'F' or 'a'-'f' bytes.

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #9

          @KroMignon said in QByteArray::fromHex(QString.toUtf8()) presenting undesired behavior:

          when reading from stream

          I wouldn't use QTextStream at all but QIODevice::readLine() to avoid the two useless conversions completely :)

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • O Offline
            O Offline
            oldevel
            wrote on last edited by
            #10

            Wow, many thanks for your reply's and suggestions. I will provide further observations shortly.

            1 Reply Last reply
            0
            • JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #11

              @KroMignon , @Christian-Ehrlicher
              Sorry, I just don't see what either of you are saying. Nothing to do with QByteArray versus QString. I simply don't understand where @Christian-Ehrlicher said:

              [T]he decoding starts from the back.

              as an answer to why the OP says the first byte he gets, which he & I expect to be the 7F at the left-hand side of the string, comes out as 0x07 [and the second character is 0xF4]

              With an odd number of characters in the string, I would have expected the first byte to be 7F and it to go wrong at the right-hand end of the string.

              Since you two seem to understand and I do not, we can leave this if you wish...

              KroMignonK Christian EhrlicherC 2 Replies Last reply
              0
              • JonBJ JonB

                @KroMignon , @Christian-Ehrlicher
                Sorry, I just don't see what either of you are saying. Nothing to do with QByteArray versus QString. I simply don't understand where @Christian-Ehrlicher said:

                [T]he decoding starts from the back.

                as an answer to why the OP says the first byte he gets, which he & I expect to be the 7F at the left-hand side of the string, comes out as 0x07 [and the second character is 0xF4]

                With an odd number of characters in the string, I would have expected the first byte to be 7F and it to go wrong at the right-hand end of the string.

                Since you two seem to understand and I do not, we can leave this if you wish...

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by
                #12

                @JonB said in QByteArray::fromHex(QString.toUtf8()) presenting undesired behavior:

                Since you two seem to understand and I do not, we can leave this if you wish...

                If you take a look at QByteArray::fromHex() source code, you will see that the string if read backwards.
                This is why he got this.

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply
                0
                • JonBJ JonB

                  @KroMignon , @Christian-Ehrlicher
                  Sorry, I just don't see what either of you are saying. Nothing to do with QByteArray versus QString. I simply don't understand where @Christian-Ehrlicher said:

                  [T]he decoding starts from the back.

                  as an answer to why the OP says the first byte he gets, which he & I expect to be the 7F at the left-hand side of the string, comes out as 0x07 [and the second character is 0xF4]

                  With an odd number of characters in the string, I would have expected the first byte to be 7F and it to go wrong at the right-hand end of the string.

                  Since you two seem to understand and I do not, we can leave this if you wish...

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #13

                  @JonB said in QByteArray::fromHex(QString.toUtf8()) presenting undesired behavior:

                  I simply don't understand where @Christian-Ehrlicher said:

                  Hehe.
                  It was the only meaningful explanation for the described problem and when you look at the code (hey, it's opensource ;)) you see that it's the correct one.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0
                  • JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #14

                    @KroMignon , @Christian-Ehrlicher

                    If you take a look at QByteArray::fromHex() source code, you will see that the string if read backwards.

                    Indeed, that makes sense for behaviour reported, I merely expressed my surprise as it was not the direction I expected. I expected it would naturally work left-to-right, that's all!

                    So with a missing byte at the end the code does not allow decoding of all the bytes up to the last one, so you can mostly see what is in there, instead it makes them all wrong if, say, the input is prematurely curtailed, for whatever reason. Potentially a shame/confusing. That's all. I agree the

                    the documentation is explicit: Input is not checked for validity

                    means it can do what it likes with this bad input, as I say I was merely surprised that it does turn out to do right-to-left.....

                    1 Reply Last reply
                    0
                    • O Offline
                      O Offline
                      oldevel
                      wrote on last edited by
                      #15

                      I gained insight from all your posts. It turns out that simply correcting the line length sorted the problem, thank you.

                      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