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

Convert QbyteArray to qreal

Scheduled Pinned Locked Moved Solved General and Desktop
38 Posts 7 Posters 4.4k Views 1 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.
  • D dziko147

    @KroMignon Exact . I write manually this test data .
    @Christian-Ehrlicher I will receive a frame using QCanBusFrame , So the data will be in a QByteArray variable . I expect a Rpm Value wich is real .

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

    @dziko147 said in Convert QbyteArray to qreal:

    Exact . I write manually this test data .

    I don't know how to say it more clearly:

    • What does this represent?
    • Is it a hexadecimal representation of your test data?
    • CAN frame have a maximum of 8 Byte of payload, this is much more data, so what is it?

    You should be able to answer those questions, or you are not knowing what you are doing. Which is very bad.

    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
    2
    • D Offline
      D Offline
      dziko147
      wrote on last edited by
      #23

      @Christian-Ehrlicher I do not know how the sender encodes the data .
      What I know is I receive data using the QCanBusFrame . then I extract the data .
      So i get a QByteArray Data . I want to convert it .

      JonBJ jsulmJ 2 Replies Last reply
      0
      • D Offline
        D Offline
        dziko147
        wrote on last edited by
        #24

        @KroMignon Can you give me an example of a QByteArray compouned of 5bytes ?

        KroMignonK 1 Reply Last reply
        0
        • D dziko147

          @KroMignon Can you give me an example of a QByteArray compouned of 5bytes ?

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

          @dziko147 said in Convert QbyteArray to qreal:

          Can you give me an example of a QByteArray compouned of 5bytes ?

          Yes, here are many examples:

          QByteArray b1("ABCDE");
          QByteArray b2;
          b2.resize(5);
          b2[0] = 0x3c;
          b2[1] = 0xb8;
          b2[2] = 0x64;
          b2[3] = 0x18;
          b3[4] = 0xca;
          
          QByteArray b3 = QByteArray:fromHex("0102030405");
          

          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
          3
          • D dziko147

            @Christian-Ehrlicher I do not know how the sender encodes the data .
            What I know is I receive data using the QCanBusFrame . then I extract the data .
            So i get a QByteArray Data . I want to convert it .

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

            @dziko147 said in Convert QbyteArray to qreal:

            @Christian-Ehrlicher I do not know how the sender encodes the data .

            If you do not know how the data is encoded/sent, @Christian-Ehrlicher does not....

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dziko147
              wrote on last edited by
              #27

              @KroMignon
              I get value =0 after converting :/

              JonBJ KroMignonK 2 Replies Last reply
              0
              • D dziko147

                @Christian-Ehrlicher I do not know how the sender encodes the data .
                What I know is I receive data using the QCanBusFrame . then I extract the data .
                So i get a QByteArray Data . I want to convert it .

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

                @dziko147 said in Convert QbyteArray to qreal:

                I do not know how the sender encodes the data

                Then how do you want to decode it?
                You should at least get some data from sender where you know what exact numbers the sender sent, then you can try to find out how the data was encoded.

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

                1 Reply Last reply
                0
                • D dziko147

                  @KroMignon
                  I get value =0 after converting :/

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

                  @dziko147
                  After converting.... what? how?

                  Tell you what: tell us two things:

                  1. Forget about converting anything to float. Show us what the values of the first 5 bytes you receive are. (You say the first byte is a status byte and the next 4 bytes are the value you are interested in, right?)
                  2. Tell us what floating point number you know that encodes/conveys/sends. (Please make sure it is not 0!)
                  1 Reply Last reply
                  1
                  • D dziko147

                    @KroMignon
                    I get value =0 after converting :/

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

                    @dziko147 said in Convert QbyteArray to qreal:

                    I get value =0 after converting :/

                    After converting what?
                    Please, take time to understand what are the data you want to convert.
                    I cannot do this for you!

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

                    D 1 Reply Last reply
                    1
                    • KroMignonK KroMignon

                      @dziko147 said in Convert QbyteArray to qreal:

                      I get value =0 after converting :/

                      After converting what?
                      Please, take time to understand what are the data you want to convert.
                      I cannot do this for you!

                      D Offline
                      D Offline
                      dziko147
                      wrote on last edited by
                      #31

                      @KroMignon
                      QByteArray rpmdata = QByteArray::fromHex("0102030405"); ---> I want to
                      convert this .
                      qDebug() << rpmdata << "this is data" ;
                      QDataStream stream(rpmdata);
                      quint8 byte;
                      float rpmfloat;
                      stream >> byte; // skip first byte
                      stream >> rpmfloat; // read float value
                      qDebug() << rpmfloat << "this is data to float" ;
                      back.setValue(rpmfloat); ----> I display the value converted here .

                      JonBJ KroMignonK 2 Replies Last reply
                      0
                      • D dziko147

                        @KroMignon
                        QByteArray rpmdata = QByteArray::fromHex("0102030405"); ---> I want to
                        convert this .
                        qDebug() << rpmdata << "this is data" ;
                        QDataStream stream(rpmdata);
                        quint8 byte;
                        float rpmfloat;
                        stream >> byte; // skip first byte
                        stream >> rpmfloat; // read float value
                        qDebug() << rpmfloat << "this is data to float" ;
                        back.setValue(rpmfloat); ----> I display the value converted here .

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

                        @dziko147
                        You push raw bytes into a QDataStream. You then try to read that back as, say, a float.

                        Unless I have misunderstood QDataStream you cannot do that. It is a structured stream. E.g. it will (well, should) contain "markers" for the data and its types.

                        Experts, I am right here, aren't I (have never used QDataStream)?

                        UPDATE
                        For anyone reading this, the expert answers below indicate this is not the case, and my understanding was incorrect.

                        Christian EhrlicherC KroMignonK 2 Replies Last reply
                        0
                        • JonBJ JonB

                          @dziko147
                          You push raw bytes into a QDataStream. You then try to read that back as, say, a float.

                          Unless I have misunderstood QDataStream you cannot do that. It is a structured stream. E.g. it will (well, should) contain "markers" for the data and its types.

                          Experts, I am right here, aren't I (have never used QDataStream)?

                          UPDATE
                          For anyone reading this, the expert answers below indicate this is not the case, and my understanding was incorrect.

                          Christian EhrlicherC Online
                          Christian EhrlicherC Online
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #33

                          @JonB said in Convert QbyteArray to qreal:

                          E.g. it will (well, should) contain "markers" for the data and its types.

                          Not for PODs. For QString e.g. a length field is added. What is added for which build-in Qt type is not documented - QDataStream is not meant to be used with anything else than a QDataStream.

                          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
                          1
                          • JonBJ JonB

                            @dziko147
                            You push raw bytes into a QDataStream. You then try to read that back as, say, a float.

                            Unless I have misunderstood QDataStream you cannot do that. It is a structured stream. E.g. it will (well, should) contain "markers" for the data and its types.

                            Experts, I am right here, aren't I (have never used QDataStream)?

                            UPDATE
                            For anyone reading this, the expert answers below indicate this is not the case, and my understanding was incorrect.

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

                            @JonB said in Convert QbyteArray to qreal:

                            Unless I have misunderstood QDataStream you cannot do that. It is a structured stream. E.g. it will (well, should) contain "markers" for the data and its types.

                            Of course this is possible, this is no "marker" for serializing/deserializing "base types" like float, double, q(u)int8, q(u)int16, etc.

                            But "02030405" converted to float is 9.6255135462533111609068148127E-38... almost 0 ;)

                            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
                            2
                            • D dziko147

                              @KroMignon
                              QByteArray rpmdata = QByteArray::fromHex("0102030405"); ---> I want to
                              convert this .
                              qDebug() << rpmdata << "this is data" ;
                              QDataStream stream(rpmdata);
                              quint8 byte;
                              float rpmfloat;
                              stream >> byte; // skip first byte
                              stream >> rpmfloat; // read float value
                              qDebug() << rpmfloat << "this is data to float" ;
                              back.setValue(rpmfloat); ----> I display the value converted here .

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

                              @dziko147 said in Convert QbyteArray to qreal:

                              QByteArray rpmdata = QByteArray::fromHex("0102030405"); ---> I want to
                              convert this .

                              What did you expect?
                              Again, you have to know what you are doing.

                              Here you are converting this array of bytes { 0x02, 0x03, 0x04, 0x05 } to float.
                              which is 9.6255135462533111609068148127E-38, almost 0.

                              If you change the array to something else like "014F030405", which means converting { 0x4f, 0x03, 0x04 , 0x05} to float, you will obtain 2.19807872E9 (almost 2.2 Billion)

                              ==> cf. https://www.binaryconvert.com/convert_float.html

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

                              D 1 Reply Last reply
                              2
                              • KroMignonK KroMignon

                                @dziko147 said in Convert QbyteArray to qreal:

                                QByteArray rpmdata = QByteArray::fromHex("0102030405"); ---> I want to
                                convert this .

                                What did you expect?
                                Again, you have to know what you are doing.

                                Here you are converting this array of bytes { 0x02, 0x03, 0x04, 0x05 } to float.
                                which is 9.6255135462533111609068148127E-38, almost 0.

                                If you change the array to something else like "014F030405", which means converting { 0x4f, 0x03, 0x04 , 0x05} to float, you will obtain 2.19807872E9 (almost 2.2 Billion)

                                ==> cf. https://www.binaryconvert.com/convert_float.html

                                D Offline
                                D Offline
                                dziko147
                                wrote on last edited by
                                #36

                                @KroMignon I tried this :
                                QByteArray rpmdata;
                                rpmdata.resize(4);
                                rpmdata[0] = 0x43;
                                rpmdata[1] = 0x96;
                                rpmdata[2] = 0x7D;
                                rpmdata[3] = 0xA6;
                                qDebug() << rpmdata << "this is data" ;

                                QDataStream stream(rpmdata);

                                float value;
                                stream >> value; // read float value
                                qDebug() << value << "this is data to float" ;

                                back.setValue(value);

                                I get :
                                deb.PNG

                                the 43967DA6 = (300.98163 ) .
                                this is the the variable rpm.PNG

                                KroMignonK 1 Reply Last reply
                                0
                                • D dziko147

                                  @KroMignon I tried this :
                                  QByteArray rpmdata;
                                  rpmdata.resize(4);
                                  rpmdata[0] = 0x43;
                                  rpmdata[1] = 0x96;
                                  rpmdata[2] = 0x7D;
                                  rpmdata[3] = 0xA6;
                                  qDebug() << rpmdata << "this is data" ;

                                  QDataStream stream(rpmdata);

                                  float value;
                                  stream >> value; // read float value
                                  qDebug() << value << "this is data to float" ;

                                  back.setValue(value);

                                  I get :
                                  deb.PNG

                                  the 43967DA6 = (300.98163 ) .
                                  this is the the variable rpm.PNG

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

                                  @dziko147 said in Convert QbyteArray to qreal:

                                  the 43967DA6 = (300.98163 ) .

                                  Sorry, my bad, it seems there are something I had forgot about QDataStream: when using float you have to specify floating point precision with stream.setFloatingPointPrecision(QDataStream::SinglePrecision);.

                                  The other way is to use qfloat16 (WRONG)

                                  QByteArray rpmdata;
                                  rpmdata.resize(4);
                                  rpmdata[0] = 0x43;
                                  rpmdata[1] = 0x96;
                                  rpmdata[2] = 0x7D;
                                  rpmdata[3] = 0xA6;
                                  qDebug() << rpmdata << "this is data" ;
                                  
                                  QDataStream stream(rpmdata);
                                  
                                  stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
                                  float value;
                                  stream >> value; // read float value
                                  qDebug() << value << "this is data to float" ;
                                  
                                  back.setValue(value);
                                  

                                  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
                                  4
                                  • D Offline
                                    D Offline
                                    dziko147
                                    wrote on last edited by
                                    #38

                                    @KroMignon thank you it works :) .

                                    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