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. Problem converting QByteArray to long

Problem converting QByteArray to long

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 7 Posters 4.5k 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
    mpergand
    wrote on 21 Jun 2018, 17:58 last edited by
    #7

    What about using QDataStream:

    QDataStream data(msg);
    qint64 val;
    data>>val;
    

    QDataStream is smart enough to convert to the host endianess automaticaly.
    By defaut, DataStream is set to big endian, see setByteOrder().

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 21 Jun 2018, 19:28 last edited by
      #8

      @mpergand said in Problem converting QByteArray to long:

      What about using QDataStream

      Since QDataStream is a stream format it will not only store the 4 or 8 bytes but also some information to know what is saved - therefore it's totally useless here.

      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
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 21 Jun 2018, 19:58 last edited by
        #9

        Hi,

        Another important point, you don't check that you received all your data. The readyRead doesn't signal that the socket got the complete payload, only there's something that arrived. Your payload might be small enough now but it might very well change in the near future.

        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
        2
        • J JonB
          21 Jun 2018, 16:26

          @nico162
          First let's start by having your actual code. Do you really have:

              for(int i=0;i<8;i++){
                  qInfo()<<(qint64)hashKeyValue[0];
              }
          

          or would you like to correct that in either code or pasting here?

          Second, have a look at the docs & example for http://doc.qt.io/qt-5/qbytearray.html#toLong. It's not doing anything like you're expecting it to do: it's for parsing a byte-array-string to a long, not creating a long out of a number already sitting there in binary bytes!

          N Offline
          N Offline
          nico162
          wrote on 22 Jun 2018, 07:36 last edited by
          #10

          @JonB

          my bad, no it's
          for(int i=0;i<8;i++){
          qInfo()<<(qint64)hashKeyValue[i];
          }

          Ah okay.. Thank you

          J 1 Reply Last reply 22 Jun 2018, 07:38
          0
          • N nico162
            22 Jun 2018, 07:36

            @JonB

            my bad, no it's
            for(int i=0;i<8;i++){
            qInfo()<<(qint64)hashKeyValue[i];
            }

            Ah okay.. Thank you

            J Offline
            J Offline
            JonB
            wrote on 22 Jun 2018, 07:38 last edited by
            #11

            @nico162
            That's why it's always best to copy & paste code, not type it in, when you want help! :)

            1 Reply Last reply
            0
            • V VRonin
              21 Jun 2018, 16:54

              First of all, you need to understand endianness

              Matching endianness between java and C++

              
              void Receiver::on_receivingByteBuffMessage(const QByteArray& msg){
              Q_ASSERT(msg.size()==sizeof(qint64));
              qint64 value = *reinterpret_cast<const qint64*>(msg.constData());
              }
              

              Mismatching endianness between java and C++

              void Receiver::on_receivingByteBuffMessage(const QByteArray& msg){
              Q_ASSERT(msg.size()==sizeof(qint64));
              QByteArray reversed(sizeof(qint64),0);
              std::copy(msg.crbegin(),msg.crend(),reversed.begin());
              qint64 value = *reinterpret_cast<const qint64*>(reversed.constData());
              }
              
              N Offline
              N Offline
              nico162
              wrote on 22 Jun 2018, 07:49 last edited by
              #12

              @VRonin
              Yes I though it could be that, but the bytes sent from Java to C++, are of same value and in the same order at the sending and receiving (I print the content in java and in C++, so I know the structure of the byte array).
              So I admit that it wasn't an endianness error (maybe I am wrong ..).

              1 Reply Last reply
              0
              • V VRonin
                21 Jun 2018, 16:54

                First of all, you need to understand endianness

                Matching endianness between java and C++

                
                void Receiver::on_receivingByteBuffMessage(const QByteArray& msg){
                Q_ASSERT(msg.size()==sizeof(qint64));
                qint64 value = *reinterpret_cast<const qint64*>(msg.constData());
                }
                

                Mismatching endianness between java and C++

                void Receiver::on_receivingByteBuffMessage(const QByteArray& msg){
                Q_ASSERT(msg.size()==sizeof(qint64));
                QByteArray reversed(sizeof(qint64),0);
                std::copy(msg.crbegin(),msg.crend(),reversed.begin());
                qint64 value = *reinterpret_cast<const qint64*>(reversed.constData());
                }
                
                N Offline
                N Offline
                nico162
                wrote on 22 Jun 2018, 08:17 last edited by
                #13

                @VRonin
                Ah no okay I misunderstood... Now I see what you mean :D! And it works fine

                N 1 Reply Last reply 22 Jun 2018, 09:14
                0
                • N nico162
                  22 Jun 2018, 08:17

                  @VRonin
                  Ah no okay I misunderstood... Now I see what you mean :D! And it works fine

                  N Offline
                  N Offline
                  nico162
                  wrote on 22 Jun 2018, 09:14 last edited by nico162
                  #14

                  @nico162 said in Problem converting QByteArray to long:

                  @VRonin
                  Ah no okay I misunderstood... Now I see what you mean :D! And it works fine

                  So as @JonB explained the QByteArray::toLong() is not adapted to my need (to convert a byte array to long).
                  @VRonin gave a solution to interpret the byteArray in the correct endianness mode (from Java to C++):

                  void Receiver::on_receivingByteBuffMessage(const QByteArray& msg){
                  Q_ASSERT(msg.size()==sizeof(qint64));
                  QByteArray reversed(sizeof(qint64),0);
                  std::copy(msg.crbegin(),msg.crend(),reversed.begin());
                  qint64 value = *reinterpret_cast<const qint64*>(reversed.constData());
                  }
                  

                  Maybe better solution could exist to avoid create a temp array to copy the byte in the reverse order as mentioned @JonB .
                  I personally have little bytebuffer received, so the solution that @VRonin gave it's enough efficient in my use case, that's why I close this topic.
                  @SGaist I take in account your remark thank you :).
                  I thank you all for your help :)! First time I posted a topic, and I am happy to see that there is an helpful and comprehensive community.

                  See ya !

                  1 Reply Last reply
                  1
                  • J JonB
                    21 Jun 2018, 17:15

                    @VRonin
                    Your "reversing" code involves actually copying the bytes in reverse order into a temporary area to "convert" it to a native int64. This is hideously inefficient if you have a file with billions of numbers in it :) Isn't there a solution which swaps bytes directly (e.g. so could be done on a register directly)?

                    J Offline
                    J Offline
                    JohanSolo
                    wrote on 22 Jun 2018, 09:45 last edited by
                    #15

                    @JonB said in Problem converting QByteArray to long:

                    Isn't there a solution which swaps bytes directly (e.g. so could be done on a register directly)?

                    Would by chance _byteswap_uint64 or __builtin_bswap64 do the trick?

                    `They did not know it was impossible, so they did it.'
                    -- Mark Twain

                    J 1 Reply Last reply 22 Jun 2018, 09:51
                    0
                    • J JohanSolo
                      22 Jun 2018, 09:45

                      @JonB said in Problem converting QByteArray to long:

                      Isn't there a solution which swaps bytes directly (e.g. so could be done on a register directly)?

                      Would by chance _byteswap_uint64 or __builtin_bswap64 do the trick?

                      J Offline
                      J Offline
                      JonB
                      wrote on 22 Jun 2018, 09:51 last edited by
                      #16

                      @JohanSolo
                      They would indeed, but each of those is MSVC/GCC specific ... :(

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        JohanSolo
                        wrote on 22 Jun 2018, 09:59 last edited by
                        #17

                        @JonB
                        I know, that's why I gave the two of them... Not the first time a #ifdef WIN32 would help.

                        `They did not know it was impossible, so they did it.'
                        -- Mark Twain

                        J 1 Reply Last reply 22 Jun 2018, 10:01
                        0
                        • J JohanSolo
                          22 Jun 2018, 09:59

                          @JonB
                          I know, that's why I gave the two of them... Not the first time a #ifdef WIN32 would help.

                          J Offline
                          J Offline
                          JonB
                          wrote on 22 Jun 2018, 10:01 last edited by JonB
                          #18

                          @JohanSolo
                          Yeah, but I think (for Qt) we still have to support MinGW and whatever on MacOS and ...

                          Unlike the functions for swapping 16 or 32 bytes, I couldn't find standard ones for 64 bytes anywhere when I searched. Shame!

                          1 Reply Last reply
                          0

                          16/18

                          22 Jun 2018, 09:51

                          • Login

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