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.
  • 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 Offline
    J.HilkJ Offline
    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 Offline
              Christian EhrlicherC Offline
              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
                  • 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 .

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #21

                    @dziko147 said in Convert QbyteArray to qreal:

                    . I expect a Rpm Value wich is real .

                    This tells us nothing. real just tells that the value is a floating-point value. It does not tell how this value is encoded and since you can't/won't tell us this you have to find it out by yourself. Two possibilities were already given to you, other encodings are unknown to us and therefore you're on your own when none of them works.

                    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 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 Offline
                              JonBJ Offline
                              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 Offline
                                    JonBJ Offline
                                    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 Offline
                                          JonBJ Offline
                                          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

                                          • Login

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