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

Convert float to QbyteArray

Scheduled Pinned Locked Moved General and Desktop
18 Posts 5 Posters 13.2k 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.
  • 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