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. Converting a QByteArray in Hexa format into an Int

Converting a QByteArray in Hexa format into an Int

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 7 Posters 2.4k 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.
  • P Pantoufle
    25 May 2023, 14:03

    @Pantoufle

    Forgot the output :

    My data : 8
    My data int : 19102
    
    M Offline
    M Offline
    mpergand
    wrote on 25 May 2023, 14:15 last edited by
    #14

    @Pantoufle
    What is the format of the original data ?
    ASCII in hex format ?
    Binary in big endian ?
    Other ?

    P 1 Reply Last reply 25 May 2023, 16:11
    0
    • P Pantoufle
      25 May 2023, 14:02

      @jsulm

      Yes but the code u just send is a 8 bytes QbyteArray :

      QByteArray mydata("00004A9E");
          std::cout << "My data : " << mydata.size() << std::endl;
      
          bool ok;
          int a = mydata.toInt(&ok, 16);
          if(!ok)
              std::cout << "Fail convert" << std::endl;
      
          std::cout << "My data int " << a << std::endl;
      
      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 25 May 2023, 14:21 last edited by J.Hilk
      #15

      @Pantoufle this is probably through the ambiguity of the QByteArray constructor, wich builds down to char* or an other QByteArray

      use the proper constructor from a String, and it should be fine:

      auto mydata = QString("00004A9E").toLatin1();
          std::cout << "My data : " << mydata.size() << std::endl;
      
          bool ok;
          int a = mydata.toInt(&ok, 16);
          if(!ok)
              std::cout << "Fail convert" << std::endl;
      
          std::cout << "My data int " << a << std::endl;
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • M mpergand
        25 May 2023, 14:15

        @Pantoufle
        What is the format of the original data ?
        ASCII in hex format ?
        Binary in big endian ?
        Other ?

        P Offline
        P Offline
        Pantoufle
        wrote on 25 May 2023, 16:11 last edited by
        #16

        @mpergand said in Converting a QByteArray in Hexa format into an Int:

        @Pantoufle
        What is the format of the original data ?
        ASCII in hex format ?
        Binary in big endian ?
        Other ?

        Binary in big endian

        @J-Hilk It works but same problem as before, the QByteArray generated by the line (auto mydata = QString("00004A9E").toLatin1();) is 8 bytes long.

        C J 2 Replies Last reply 25 May 2023, 16:39
        0
        • P Pantoufle
          25 May 2023, 16:11

          @mpergand said in Converting a QByteArray in Hexa format into an Int:

          @Pantoufle
          What is the format of the original data ?
          ASCII in hex format ?
          Binary in big endian ?
          Other ?

          Binary in big endian

          @J-Hilk It works but same problem as before, the QByteArray generated by the line (auto mydata = QString("00004A9E").toLatin1();) is 8 bytes long.

          C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 25 May 2023, 16:39 last edited by
          #17

          @Pantoufle said in Converting a QByteArray in Hexa format into an Int:

          the QByteArray generated by the line (auto mydata = QString("00004A9E").toLatin1();) is 8 bytes long.

          But that's correct - you pass 8 bytes to a QString ctor and convert it back to a QByteArray.

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

          P 1 Reply Last reply 26 May 2023, 08:31
          1
          • P Pantoufle
            25 May 2023, 16:11

            @mpergand said in Converting a QByteArray in Hexa format into an Int:

            @Pantoufle
            What is the format of the original data ?
            ASCII in hex format ?
            Binary in big endian ?
            Other ?

            Binary in big endian

            @J-Hilk It works but same problem as before, the QByteArray generated by the line (auto mydata = QString("00004A9E").toLatin1();) is 8 bytes long.

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 26 May 2023, 05:09 last edited by
            #18

            @Pantoufle said in Converting a QByteArray in Hexa format into an Int:

            Binary in big endian

            Then why do you pass a string to QByteArray?
            You should rather use https://doc.qt.io/qt-6/qbytearray.html#fromRawData
            Also see https://doc.qt.io/qt-6/qtendian.html

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • C Christian Ehrlicher
              25 May 2023, 16:39

              @Pantoufle said in Converting a QByteArray in Hexa format into an Int:

              the QByteArray generated by the line (auto mydata = QString("00004A9E").toLatin1();) is 8 bytes long.

              But that's correct - you pass 8 bytes to a QString ctor and convert it back to a QByteArray.

              P Offline
              P Offline
              Pantoufle
              wrote on 26 May 2023, 08:31 last edited by
              #19

              @Christian-Ehrlicher

              My case is equivalent to :

              QByteArray mydata = QByteArray::fromHex("0000049E");

              Which generate a 4 bytes array, so yeah it's normal that it generates a 8 bytes in the previous example but just it's not what I want.

              @jsulm

              QByteArray mydata = QByteArray::fromHex("0000049E");
              Represent a binary in big endian, 4 bytes long.
              I didn't know about QtEndian and fromRawData, I will check that thx.
              Also I found that it works with QByteArrayLiteral("\x00\x00\x04\x9E")

              J 1 Reply Last reply 26 May 2023, 08:40
              0
              • P Pantoufle
                26 May 2023, 08:31

                @Christian-Ehrlicher

                My case is equivalent to :

                QByteArray mydata = QByteArray::fromHex("0000049E");

                Which generate a 4 bytes array, so yeah it's normal that it generates a 8 bytes in the previous example but just it's not what I want.

                @jsulm

                QByteArray mydata = QByteArray::fromHex("0000049E");
                Represent a binary in big endian, 4 bytes long.
                I didn't know about QtEndian and fromRawData, I will check that thx.
                Also I found that it works with QByteArrayLiteral("\x00\x00\x04\x9E")

                J Offline
                J Offline
                JonB
                wrote on 26 May 2023, 08:40 last edited by JonB
                #20

                @Pantoufle
                Hi. I don't understand if you still have a problem or a question? You want a 4 byte integer, and you have one. I don't understand what the relevance is of a string you choose to pass to QByteArray::fromHex() as a convenient way of getting the 4 bytes into memory, who cares what the string is or how long it is? It has nothing to do with how your device works, which does not use hex or strings, it just sends a 4-byte integer. Which should be convertible via QByteArray::toInt() provided the byte order matches your machine's endian order (else it would need swapping).

                My bad, see @Christian-Ehrlicher's clarification of QByteArray::toInt() below!

                C 1 Reply Last reply 26 May 2023, 08:46
                0
                • J JonB
                  26 May 2023, 08:40

                  @Pantoufle
                  Hi. I don't understand if you still have a problem or a question? You want a 4 byte integer, and you have one. I don't understand what the relevance is of a string you choose to pass to QByteArray::fromHex() as a convenient way of getting the 4 bytes into memory, who cares what the string is or how long it is? It has nothing to do with how your device works, which does not use hex or strings, it just sends a 4-byte integer. Which should be convertible via QByteArray::toInt() provided the byte order matches your machine's endian order (else it would need swapping).

                  My bad, see @Christian-Ehrlicher's clarification of QByteArray::toInt() below!

                  C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 26 May 2023, 08:46 last edited by
                  #21

                  @JonB said in Converting a QByteArray in Hexa format into an Int:

                  Which should be convertible via QByteArray::toInt() provided the byte order matches your machine's endian order (else it would need swapping).

                  No, you're wrong here.
                  QByteArray::toInt() converts a string representation of a number to an integer.

                  int32_t val = *(reinterpret_cast<int32_t*>(ba.constData()))
                  

                  if the endian is the same on the source and destination, otherwise use the qEndian helper functions.

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

                  J P 2 Replies Last reply 26 May 2023, 08:49
                  1
                  • C Christian Ehrlicher
                    26 May 2023, 08:46

                    @JonB said in Converting a QByteArray in Hexa format into an Int:

                    Which should be convertible via QByteArray::toInt() provided the byte order matches your machine's endian order (else it would need swapping).

                    No, you're wrong here.
                    QByteArray::toInt() converts a string representation of a number to an integer.

                    int32_t val = *(reinterpret_cast<int32_t*>(ba.constData()))
                    

                    if the endian is the same on the source and destination, otherwise use the qEndian helper functions.

                    J Offline
                    J Offline
                    JonB
                    wrote on 26 May 2023, 08:49 last edited by
                    #22

                    @Christian-Ehrlicher
                    Oh, I am so wrong, I should have looked at QByteArray::toInt() docs before answering instead of assuming it was a binary operation! I now understand where the confusion lies totally! I will cross out my earlier.

                    1 Reply Last reply
                    0
                    • C Christian Ehrlicher
                      26 May 2023, 08:46

                      @JonB said in Converting a QByteArray in Hexa format into an Int:

                      Which should be convertible via QByteArray::toInt() provided the byte order matches your machine's endian order (else it would need swapping).

                      No, you're wrong here.
                      QByteArray::toInt() converts a string representation of a number to an integer.

                      int32_t val = *(reinterpret_cast<int32_t*>(ba.constData()))
                      

                      if the endian is the same on the source and destination, otherwise use the qEndian helper functions.

                      P Offline
                      P Offline
                      Paul Colby
                      wrote on 27 May 2023, 07:47 last edited by
                      #23

                      Just use the Qt endian functions every time, since they evaluate to a no-op (ie static_cast) when the source and destination match anyway. Eg:

                      const QByteArray mydata = QByteArray::fromHex("00004A9E");
                      const quint32 mydataInteger = qFromBigEndian<quint32>(mydata);
                      qDebug() << mydata;
                      qDebug() << mydataInteger;
                      

                      Outputs:

                      "\x00\x00J\x9E"
                      19102
                      

                      Cheers.

                      1 Reply Last reply
                      1

                      23/23

                      27 May 2023, 07:47

                      • Login

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