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

Conversion from QByteArray to int

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 6 Posters 25.2k Views 3 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.
  • M mulfycrowh

    I tried something else that runs pretty well but in the wrong order:

    info.toHex().toInt(&ok, 16);
    

    The result is 256.
    It means that it has been read in the wrong order.
    How can I specify that first byte is LSB and second one MSB ?

    Paul ColbyP Offline
    Paul ColbyP Offline
    Paul Colby
    wrote on last edited by
    #5

    @mulfycrowh said in Conversion from QByteArray to int:

    info.toHex().toInt(&ok, 16);
    

    The intermediate conversion to hex is unnecessary.

    The result is 256. It means that it has been read in the wrong order.

    Whether that code will produce the right result or not, depends on the endianness of the host. In your case, as you said, its the wrong order. On another platform, it would be the correct order.

    How can I specify that first byte is LSB and second one MSB

    Exactly as I suggested above :)

    #include <QtEndian>
    ...
    const int count = qFromLittleEndian<qint16>(info.data());
    

    Here the qFromLittleEndian function knows how to convert from little-endian (least significant byte first), to whatever endianness your host is (which may or may not be the same endianness).

    Give it a go.

    Cheers.

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

      I tried what you suggested:

      qFromLittleEndian<quint16>(info.data());
      

      I've got the compiler error :

      C2665 'qFromLittleEndian': none of the 2 overloads could convert all the argument types

      Paul ColbyP 1 Reply Last reply
      0
      • M Offline
        M Offline
        mulfycrowh
        wrote on last edited by
        #7

        I mean qint16.
        Same issue

        1 Reply Last reply
        0
        • M mulfycrowh

          I tried what you suggested:

          qFromLittleEndian<quint16>(info.data());
          

          I've got the compiler error :

          C2665 'qFromLittleEndian': none of the 2 overloads could convert all the argument types

          Paul ColbyP Offline
          Paul ColbyP Offline
          Paul Colby
          wrote on last edited by Paul Colby
          #8

          @mulfycrowh said in Conversion from QByteArray to int:

          I've got the compiler error :
          C2665 'qFromLittleEndian': none of the 2 overloads could convert all the argument types

          Ah... from the qFromLittleEndian docs:

          Note: Since Qt 5.7, the type of the src parameter is a void pointer.

          For pre-5.7, you will need to cast to uchar, like:

          int count = qFromLittleEndian<qint16>(reinterpret_cast<const uchar *>(info.data()));
          

          (I just tested that on Qt 5.5.1)

          Cheers.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mulfycrowh
            wrote on last edited by
            #9

            thanks, it runs perfectly !

            kshegunovK 1 Reply Last reply
            0
            • M mulfycrowh

              thanks, it runs perfectly !

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by kshegunov
              #10

              @mulfycrowh
              A two byte sequence is a short, not int. You should use the data stream classes for such deserializations instead of going to type-safety hell:

              QByteArray data; //< This is your data
              
              QDataStream in(data); //< Attach a read-only stream to it
              in.setByteOrder(QDataStream::LittleEndian); //< Set the proper byte order
              
              qint16 result; //< The result you want
              in >> result; //< Just read it from the stream
              

              Read and abide by the Qt Code of Conduct

              S 1 Reply Last reply
              4
              • kshegunovK kshegunov

                @mulfycrowh
                A two byte sequence is a short, not int. You should use the data stream classes for such deserializations instead of going to type-safety hell:

                QByteArray data; //< This is your data
                
                QDataStream in(data); //< Attach a read-only stream to it
                in.setByteOrder(QDataStream::LittleEndian); //< Set the proper byte order
                
                qint16 result; //< The result you want
                in >> result; //< Just read it from the stream
                
                S Offline
                S Offline
                sude
                wrote on last edited by
                #11

                @kshegunov said in Conversion from QByteArray to int:

                QByteArray data; //< This is your data

                QDataStream in(data); //< Attach a read-only stream to it
                in.setByteOrder(QDataStream::LittleEndian); //< Set the proper byte order

                qint16 result; //< The result you want
                in >> result; //< Just read it from the stream

                Hi i want to read more than one number and i can read only first number how can i read all of them plese help me :) !!

                JonBJ 1 Reply Last reply
                0
                • S sude

                  @kshegunov said in Conversion from QByteArray to int:

                  QByteArray data; //< This is your data

                  QDataStream in(data); //< Attach a read-only stream to it
                  in.setByteOrder(QDataStream::LittleEndian); //< Set the proper byte order

                  qint16 result; //< The result you want
                  in >> result; //< Just read it from the stream

                  Hi i want to read more than one number and i can read only first number how can i read all of them plese help me :) !!

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #12

                  @sude
                  Execute a further in >> something operation to read more data. What else is there to say?

                  1 Reply Last reply
                  1
                  • S Offline
                    S Offline
                    sude
                    wrote on last edited by
                    #13

                    It isnt work, i still read first number of array :(

                    JonBJ 1 Reply Last reply
                    0
                    • S sude

                      It isnt work, i still read first number of array :(

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #14

                      @sude
                      It does work. So your code is wrong, or your data is not what you expect, or you are not looking at the right thing. Suggest you open your own new topic for this and post your problem/code; this topic is very old.

                      S 1 Reply Last reply
                      1
                      • JonBJ JonB

                        @sude
                        It does work. So your code is wrong, or your data is not what you expect, or you are not looking at the right thing. Suggest you open your own new topic for this and post your problem/code; this topic is very old.

                        S Offline
                        S Offline
                        sude
                        wrote on last edited by
                        #15

                        @JonB OK, thanks a lot. I'm appriciate your answer. I will create new topic. :)

                        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