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
Forum Updated to NodeBB v4.3 + New Features

Converting a QByteArray in Hexa format into an Int

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 7 Posters 2.8k 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.
  • P Offline
    P Offline
    Pantoufle
    wrote on last edited by
    #10

    I've found an answer that works :

    QByteArray mydata = QByteArray::fromHex("00004A9E");
    QDataStream ds(mydata);
    ds.setByteOrder(QDataStream::BigEndian);
    int mydataInteger;
    ds >> mydataInteger;
    std::cout << "Int content : " << mydataInteger << std::endl;
    

    But I would be glad if you can make me understand why toInt() don't work.

    jsulmJ 1 Reply Last reply
    0
    • P Pantoufle

      I've found an answer that works :

      QByteArray mydata = QByteArray::fromHex("00004A9E");
      QDataStream ds(mydata);
      ds.setByteOrder(QDataStream::BigEndian);
      int mydataInteger;
      ds >> mydataInteger;
      std::cout << "Int content : " << mydataInteger << std::endl;
      

      But I would be glad if you can make me understand why toInt() don't work.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #11

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

      why toInt() don't work

      This code prints 19102:

      QByteArray n("00004A9E");
      bool ok;
      qDebug() << n.toInt(&ok, 16);
      

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

      P 1 Reply Last reply
      0
      • jsulmJ jsulm

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

        why toInt() don't work

        This code prints 19102:

        QByteArray n("00004A9E");
        bool ok;
        qDebug() << n.toInt(&ok, 16);
        
        P Offline
        P Offline
        Pantoufle
        wrote on last edited by
        #12

        @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;
        
        P J.HilkJ 2 Replies Last reply
        0
        • P Pantoufle

          @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;
          
          P Offline
          P Offline
          Pantoufle
          wrote on last edited by
          #13

          @Pantoufle

          Forgot the output :

          My data : 8
          My data int : 19102
          
          M 1 Reply Last reply
          0
          • P Pantoufle

            @Pantoufle

            Forgot the output :

            My data : 8
            My data int : 19102
            
            M Offline
            M Offline
            mpergand
            wrote on 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
            0
            • P Pantoufle

              @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.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on 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

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

                Christian EhrlicherC jsulmJ 2 Replies Last reply
                0
                • P Pantoufle

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

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 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
                  1
                  • P Pantoufle

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

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 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
                    • Christian EhrlicherC Christian Ehrlicher

                      @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 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")

                      JonBJ 1 Reply Last reply
                      0
                      • P Pantoufle

                        @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")

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 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!

                        Christian EhrlicherC 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @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!

                          Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 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

                          JonBJ Paul ColbyP 2 Replies Last reply
                          1
                          • Christian EhrlicherC Christian Ehrlicher

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

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on 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
                            • Christian EhrlicherC Christian Ehrlicher

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

                              Paul ColbyP Offline
                              Paul ColbyP Offline
                              Paul Colby
                              wrote on 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

                              • Login

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