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

Convert QbyteArray to qreal

Scheduled Pinned Locked Moved Solved General and Desktop
38 Posts 7 Posters 3.7k 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.
  • D Offline
    D Offline
    dziko147
    wrote on last edited by
    #1

    Hello ,
    Iwant to convert a QbyteArray to qreal .

    I tried many solution but usually i get value = 0 ;

    I read in documentation that i should use a QDataStream .

    Does Anyone know how to solve this problem :)

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Its impossible to guess for anyone as it really, depends on what is inside the QbyteArray.
      If its related to the QCanBusFrame thing, then I suggest trying to dump the payload and see what is inside.
      Only you can know.

      We cannot convert some random amount of bytes to a qreal as it would not be correct.

      Does to int give a valid value ? Are you sure ther eeven IS a qreal value send from the other end ?

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

        @mrjj yes its a payload of a Can bus frame .
        the can bus send 5byte .
        And the type is real .

        J.HilkJ 1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @dziko147 said in Convert QbyteArray to qreal:

          5byte .
          And the type is real .

          No. A standard c real value is either 4 or 8 bytes. If you get 5 bytes you have to interpret them by your own based on the spec from your sending device.

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

            @mrjj yes its a payload of a Can bus frame .
            the can bus send 5byte .
            And the type is real .

            J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @dziko147 said in Convert QbyteArray to qreal:

            @mrjj yes its a payload of a Can bus frame .
            the can bus send 5byte .
            And the type is real .

            Great, only 120 possible permutations.

            You'll need more information from the sender side.


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

              the frame sent is compouned of 1byte of bus status and then 4 byte for the data (the real variable) .
              So in this case what should I do ?

              J.HilkJ 1 Reply Last reply
              0
              • D dziko147

                the frame sent is compouned of 1byte of bus status and then 4 byte for the data (the real variable) .
                So in this case what should I do ?

                J.HilkJ Online
                J.HilkJ Online
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @dziko147 there are tons of ways,

                • I personally would prefer QDataStream

                • Quick and dirty:

                //QByteArray frame;
                    float value{0};
                    memcpy(&value, frame.data()+1, 4);
                

                50/50 that this would result in the "correct" float


                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.

                JonBJ 1 Reply Last reply
                3
                • D Offline
                  D Offline
                  dziko147
                  wrote on last edited by
                  #8

                  @J-Hilk thank you
                  Can you please provide an example with QDataStream :)

                  KroMignonK 1 Reply Last reply
                  0
                  • J.HilkJ J.Hilk

                    @dziko147 there are tons of ways,

                    • I personally would prefer QDataStream

                    • Quick and dirty:

                    //QByteArray frame;
                        float value{0};
                        memcpy(&value, frame.data()+1, 4);
                    

                    50/50 that this would result in the "correct" float

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @J-Hilk said in Convert QbyteArray to qreal:

                    memcpy(&value, frame.data()+1, 4);

                    OOI, would you really write 4 as you have done for the size? (I admit I know nothing about what a bus-frame is!)

                    J.HilkJ 1 Reply Last reply
                    1
                    • Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @JonB said in Convert QbyteArray to qreal:

                      OOI, would you really write 4 as you have done for the size?

                      Because of 'dirty' :D

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

                        @J-Hilk thank you
                        Can you please provide an example with QDataStream :)

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

                        @dziko147 said in Convert QbyteArray to qreal:

                        Can you please provide an example with QDataStream :)

                        It is so hard to read documentation?

                        //QByteArray frame
                        
                        QDataStream stream(frame);
                        
                        quint8 byte;
                        float value;
                        stream >> byte; // skip first byte
                        stream >> value: // read float 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)

                        Christian EhrlicherC 1 Reply Last reply
                        2
                        • KroMignonK KroMignon

                          @dziko147 said in Convert QbyteArray to qreal:

                          Can you please provide an example with QDataStream :)

                          It is so hard to read documentation?

                          //QByteArray frame
                          
                          QDataStream stream(frame);
                          
                          quint8 byte;
                          float value;
                          stream >> byte; // skip first byte
                          stream >> value: // read float value
                          
                          Christian EhrlicherC Online
                          Christian EhrlicherC Online
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          And only one of the two examples will give the 'correct' real value :D

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

                            @J-Hilk said in Convert QbyteArray to qreal:

                            memcpy(&value, frame.data()+1, 4);

                            OOI, would you really write 4 as you have done for the size? (I admit I know nothing about what a bus-frame is!)

                            J.HilkJ Online
                            J.HilkJ Online
                            J.Hilk
                            Moderators
                            wrote on last edited by
                            #13

                            @JonB you will have noticed, I omitted also any kind of size checks as well :D

                            @dziko147 ,@KroMignon gave a good example

                            if you do not care about the status byte, you could also use QDataStream::skipRawData,

                            just saying :D


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

                              Still not working :/ :/
                              I get this value

                                   1.36179e+30
                              
                              1 Reply Last reply
                              0
                              • D Offline
                                D Offline
                                dziko147
                                wrote on last edited by
                                #15

                                Currently I test a static QByteArray .
                                this is my code :
                                QByteArray rpmdata = "FF10001000100010001000100010001000";
                                qDebug() << rpmdata << "this is data" ;

                                QDataStream stream(rpmdata);

                                quint8 byte;
                                qreal rpmint;
                                stream >> byte; // skip first byte
                                stream >> rpmint; // read float value
                                qDebug() << rpmint << "this is data to int" ;

                                back.setValue(rpmint);
                                
                                KroMignonK 1 Reply Last reply
                                0
                                • D dziko147

                                  Currently I test a static QByteArray .
                                  this is my code :
                                  QByteArray rpmdata = "FF10001000100010001000100010001000";
                                  qDebug() << rpmdata << "this is data" ;

                                  QDataStream stream(rpmdata);

                                  quint8 byte;
                                  qreal rpmint;
                                  stream >> byte; // skip first byte
                                  stream >> rpmint; // read float value
                                  qDebug() << rpmint << "this is data to int" ;

                                  back.setValue(rpmint);
                                  
                                  KroMignonK Offline
                                  KroMignonK Offline
                                  KroMignon
                                  wrote on last edited by
                                  #16

                                  @dziko147 said in Convert QbyteArray to qreal:

                                  Currently I test a static QByteArray .
                                  this is my code :
                                  QByteArray rpmdata = "FF10001000100010001000100010001000";

                                  What do you want to do with this?

                                  If the data are hexadecimal coded, then you have to convert them to binary:

                                  QByteArray rpmdata = QByteArray::fromHex("FF10001000100010001000100010001000");
                                  

                                  ==> https://doc.qt.io/qt-5/qbytearray.html#fromHex

                                  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
                                    #17

                                    @KroMignon
                                    So I try to receive data from QCanBusFrame .
                                    So I extract the payload using : QByteArray QCanBusFrame::payload() const .
                                    Currently I try to test a static QByteArray (I mean that i write manually the QByteArray) wich is the "rpmdata " .
                                    the type of my value is real .
                                    the frame compouned of 5byte( 1 for status and 4 for data) .
                                    I hope that my problem looks clear .

                                    KroMignonK 1 Reply Last reply
                                    0
                                    • Christian EhrlicherC Online
                                      Christian EhrlicherC Online
                                      Christian Ehrlicher
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      @dziko147 said in Convert QbyteArray to qreal:

                                      I hope that my problem looks clear .

                                      No since you still not tell us how the sender encodes the data and what value you expect.

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

                                        @KroMignon
                                        So I try to receive data from QCanBusFrame .
                                        So I extract the payload using : QByteArray QCanBusFrame::payload() const .
                                        Currently I try to test a static QByteArray (I mean that i write manually the QByteArray) wich is the "rpmdata " .
                                        the type of my value is real .
                                        the frame compouned of 5byte( 1 for status and 4 for data) .
                                        I hope that my problem looks clear .

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

                                        @dziko147 said in Convert QbyteArray to qreal:

                                        I hope that my problem looks clear .

                                        Sorry but it is not clear to me :-(

                                        What do represents "FF10001000100010001000100010001000", which is your test data?

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

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

                                          Christian EhrlicherC KroMignonK 2 Replies 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