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 float to QbyteArray
QtWS25 Last Chance

Convert float to QbyteArray

Scheduled Pinned Locked Moved General and Desktop
18 Posts 5 Posters 12.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.
  • I Offline
    I Offline
    Ivan1120
    wrote on last edited by
    #5

    We know float is 4 bytes and I just want set this 4 bytes into QbyteArray.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #6

      Did you spot QDataStream::setFloatingPointPrecision?

      1 Reply Last reply
      0
      • JeroentjehomeJ Offline
        JeroentjehomeJ Offline
        Jeroentjehome
        wrote on last edited by
        #7

        Hi,
        Floating point will never ever hold the data you set in your variable!! It's a value very very very close to it. A float is constructed by adding lots of small values together! So its like 3 + 1/16 + 1/32 + 1/128 etc etc. This placed in a variable space and done like Andre mentioned in the setFloatingPointPrecision. So don't expect the float to be only 4 numbers in your QDatastream!
        Otherwise multiply with 1000 and place in int. Then send to datastream ;-)
        Greetz

        Greetz, Jeroen

        1 Reply Last reply
        0
        • I Offline
          I Offline
          Ivan1120
          wrote on last edited by
          #8

          Sorry, I don't really understand what you mean?? I have never used Qdatastream. Do you mind give me a simple sample.following code is my test code. I don't understand why bytes of t1 and t2 is different.

          main.cpp
          @
          QByteArray t1;
          t1.append((char)0xef);
          t1.append((char)0x0b);
          float t1s = transToFloat(t1);
          QByteArray t2;
          t2.append(reinterpret_cast<const char*>(&t1s), sizeof(t1s));
          QByteArray t3;
          char* t3ptr = (char*) &t1s;
          for(int i =0; i<4; i++)
          t3.append(t3ptr[i]);
          @
          transToFloat function
          @
          float transToFloat(QByteArray data)
          {
          int addr = data[0] & 0x000000FF;
          addr |= ((data[1] << 8) & 0x0000FF00);
          float addrFloat = addr;
          return addrFloat/100;
          }
          @
          transToFloat function is found on Internet.Other people provide this method that could transform to float.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #9

            I already gave you an example. You just need to add one line to get four bytes instead of eight, and I already pointed you to to the function to use...

            1 Reply Last reply
            0
            • I Offline
              I Offline
              Ivan1120
              wrote on last edited by
              #10

              @
              float data = 3.54;
              QByteArray result;
              QDataStream s(result);
              s.setFloatingPointPrecision(QDataStream::SinglePrecision);
              s << data;
              @
              I didn't see any bytes in the result, did I do something wrong==?

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #11

                Why do you want to store a float in a QByteArray?

                You used the wrong QDataStream constructor. The one you used is for reading a QByteArray (please read the documentation at http://qt-project.org/doc/qt-5.1/qtcore/qdatastream.html ). To write, you need QDataStream(QByteArray * a, QIODevice::OpenMode mode) -- Andre's example should have two arguments.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #12

                  Sorry, I missed that one with using the wrong constructor. It was just something I quickly typed together.

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    Ivan1120
                    wrote on last edited by
                    #13

                    Hi, because I want to use QtSerialPort to write data to slave device.The one argument of QtSeriaLPort::write() is QbyteArray, so I need to transform my data to QbyteArray.
                    I change my code like follows, it is still a little weird. There are indeed four bytes in the result, but how to verify these bytes are my float variable?? I also used the other way to catch these bytes, the result is different with I use QDAtaStream to get.Which one is correct!??
                    method 1
                    @
                    float data = 3.54;
                    QByteArray result;
                    QDataStream s(&result,QIODevice::ReadWrite);
                    s.setFloatingPointPrecision(QDataStream::SinglePrecision);
                    s << data;
                    @
                    method 2
                    @
                    float data = 3.54;
                    QByteArray t2;
                    t2.append(reinterpret_cast<const char*>(&data), sizeof(data));
                    @

                    1 Reply Last reply
                    0
                    • sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by
                      #14

                      Method 3:
                      @
                      float data = 3.54;
                      QString temp = QString::number(data);
                      QByteArray result = temp.data();
                      @

                      (Z(:^

                      1 Reply Last reply
                      0
                      • I Offline
                        I Offline
                        Ivan1120
                        wrote on last edited by
                        #15

                        Sorry, the result of Method 3 is not I want... They would be ASCII "3.54", if I change data to 3.45634, result.length() would be 7,the result I want is whatever the float variable is, they only occupy four bytes in the QByteArray.

                        1 Reply Last reply
                        0
                        • I Offline
                          I Offline
                          Ivan1120
                          wrote on last edited by
                          #16

                          I know which method is I want. It's method 2. Because I use following code to verify
                          @
                          float data = 3.54;
                          QByteArray t2;
                          t2.append(reinterpret_cast<const char*>(&data), sizeof(data));
                          char tempdata[4] = {0};
                          for(int i =0; i < t2.length(); i++)
                          tempdata[i] = t2[i];
                          float answerPtr = (float)&tempdata;
                          float result = *answerPtr;
                          @
                          The result will be 3.54.Thanks for suggestion from everyone gave: ).

                          1 Reply Last reply
                          0
                          • JKSHJ Offline
                            JKSHJ Offline
                            JKSH
                            Moderators
                            wrote on last edited by
                            #17

                            [quote author="Ivan1120" date="1380167106"]I know which method is I want. It's method 2. Because I use following code to verify
                            @
                            float data = 3.54;
                            QByteArray t2;
                            t2.append(reinterpret_cast<const char*>(&data), sizeof(data));
                            char tempdata[4] = {0};
                            for(int i =0; i < t2.length(); i++)
                            tempdata[i] = t2[i];
                            float answerPtr = (float)&tempdata;
                            float result = *answerPtr;
                            @
                            The result will be 3.54.Thanks for suggestion from everyone gave: ).[/quote]
                            Also read this: http://stackoverflow.com/questions/2724359/are-there-any-modern-platforms-with-non-ieee-c-c-float-formats

                            It describes possible issues when sending float data as raw bytes between different platforms. Your master and slave might not implement float the same way.

                            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                            1 Reply Last reply
                            0
                            • I Offline
                              I Offline
                              Ivan1120
                              wrote on last edited by
                              #18

                              Thanks for your information: ), because this type of data is for our company product, i have already checked that could work at my slave device.

                              1 Reply 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