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 3.9k 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
    7 Jul 2021, 13:51

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

    K Offline
    K Offline
    KroMignon
    wrote on 7 Jul 2021, 13:58 last edited by KroMignon 7 Jul 2021, 14:00
    #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 7 Jul 2021, 14:01 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 .

      J J 2 Replies Last reply 7 Jul 2021, 14:11
      0
      • D Offline
        D Offline
        dziko147
        wrote on 7 Jul 2021, 14:02 last edited by
        #24

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

        K 1 Reply Last reply 7 Jul 2021, 14:06
        0
        • D dziko147
          7 Jul 2021, 14:02

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

          K Offline
          K Offline
          KroMignon
          wrote on 7 Jul 2021, 14:06 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
            7 Jul 2021, 14:01

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

            J Offline
            J Offline
            JonB
            wrote on 7 Jul 2021, 14:11 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 7 Jul 2021, 14:11 last edited by
              #27

              @KroMignon
              I get value =0 after converting :/

              J K 2 Replies Last reply 7 Jul 2021, 14:13
              0
              • D dziko147
                7 Jul 2021, 14:01

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

                J Online
                J Online
                jsulm
                Lifetime Qt Champion
                wrote on 7 Jul 2021, 14:12 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
                  7 Jul 2021, 14:11

                  @KroMignon
                  I get value =0 after converting :/

                  J Offline
                  J Offline
                  JonB
                  wrote on 7 Jul 2021, 14:13 last edited by JonB 7 Jul 2021, 14:15
                  #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
                    7 Jul 2021, 14:11

                    @KroMignon
                    I get value =0 after converting :/

                    K Offline
                    K Offline
                    KroMignon
                    wrote on 7 Jul 2021, 14:14 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 7 Jul 2021, 14:18
                    1
                    • K KroMignon
                      7 Jul 2021, 14:14

                      @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 7 Jul 2021, 14:18 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 .

                      J K 2 Replies Last reply 7 Jul 2021, 14:25
                      0
                      • D dziko147
                        7 Jul 2021, 14:18

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

                        J Offline
                        J Offline
                        JonB
                        wrote on 7 Jul 2021, 14:25 last edited by JonB 7 Jul 2021, 15:25
                        #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.

                        C K 2 Replies Last reply 7 Jul 2021, 14:27
                        0
                        • J JonB
                          7 Jul 2021, 14:25

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

                          C Online
                          C Online
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 7 Jul 2021, 14:27 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
                          • J JonB
                            7 Jul 2021, 14:25

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

                            K Offline
                            K Offline
                            KroMignon
                            wrote on 7 Jul 2021, 14:34 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
                              7 Jul 2021, 14:18

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

                              K Offline
                              K Offline
                              KroMignon
                              wrote on 7 Jul 2021, 14:41 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 7 Jul 2021, 15:04
                              2
                              • K KroMignon
                                7 Jul 2021, 14:41

                                @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 7 Jul 2021, 15:04 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

                                K 1 Reply Last reply 7 Jul 2021, 15:20
                                0
                                • D dziko147
                                  7 Jul 2021, 15:04

                                  @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

                                  K Offline
                                  K Offline
                                  KroMignon
                                  wrote on 7 Jul 2021, 15:20 last edited by KroMignon 7 Jul 2021, 15:22
                                  #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 7 Jul 2021, 15:25 last edited by
                                    #38

                                    @KroMignon thank you it works :) .

                                    1 Reply Last reply
                                    1

                                    31/38

                                    7 Jul 2021, 14:18

                                    • Login

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