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. QByteArray to float value
Forum Update on Monday, May 27th 2025

QByteArray to float value

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 7.1k 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.
  • beeckscheB Offline
    beeckscheB Offline
    beecksche
    wrote on last edited by beecksche
    #1

    Hello,
    from a TCP Socket i get 4 bytes representing a float value in the IEEE 754 standard.

    For example:

    QByteArray arr = socket->read(4);
    // arr.at(0) = 0x41
    // arr.at(1) = 0xf8
    // arr.at(2) = 0xf6
    // arr.at(3) = 0x00
    

    Is there an easy way to convert it? For double values i use the QDataStream like

    QByteArray arr = socket->read(8);
    
    double number;
    QDataStream(arr);
    stream >> number;
    

    But this won't work with float values.

    I have also tried

    bool ok = false;
    float val = socket->read(4).toFloat(&ok);
    // ok = false!
    

    Thanks you

    S 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        it might be horribly wrong but did you try:

        QByteArray arr = socket->read(4);
        float number =  *(reinterpret_cast<const float*>(arr.constData()));
        
        1 Reply Last reply
        1
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          Hi
          Are you sure the input is correct?
          ToFloat should work
          http://doc.qt.io/qt-5/datastreamformat.html

          float 32-bit floating point number using the standard IEEE 754 format

          Are you using QDataStream in the other end to send it so endianness is being handled for you ?

          1 Reply Last reply
          1
          • beeckscheB Offline
            beeckscheB Offline
            beecksche
            wrote on last edited by beecksche
            #5

            @mrjj
            From this converter: https://www.h-schmidt.net/FloatConverter/IEEE754.html 0x41f8f600 in decimal is 31.120117

            QByteArray arr;
            arr.resize(4);
            arr[0] = 0x41;
            arr[1] = 0xf8;
            arr[2] = 0xf6;
            arr[3] = 0x00;
            
            float val;
            QDataStream stream(arr);
            stream >> val;
            
             qDebug() << val; // val = 0
            

            No i don't convert the number. I just read them from the socket.

            mrjjM 1 Reply Last reply
            0
            • beeckscheB beecksche

              @mrjj
              From this converter: https://www.h-schmidt.net/FloatConverter/IEEE754.html 0x41f8f600 in decimal is 31.120117

              QByteArray arr;
              arr.resize(4);
              arr[0] = 0x41;
              arr[1] = 0xf8;
              arr[2] = 0xf6;
              arr[3] = 0x00;
              
              float val;
              QDataStream stream(arr);
              stream >> val;
              
               qDebug() << val; // val = 0
              

              No i don't convert the number. I just read them from the socket.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              So
              Yes, you are using QDataStream in both ends.

              And you have checked that each byte contains the same
              values when read in again as when sent?

              beeckscheB 1 Reply Last reply
              0
              • mrjjM mrjj

                So
                Yes, you are using QDataStream in both ends.

                And you have checked that each byte contains the same
                values when read in again as when sent?

                beeckscheB Offline
                beeckscheB Offline
                beecksche
                wrote on last edited by beecksche
                #7

                @mrjj

                No, i'm sorry, i'm not good in explaining. An external device sends status information through the tcp socket.

                From the manual of the device i know that the value is a floating number represented by 4 bytes. The value is displayed on the device (so i know what it should be, when i read it).

                The bytearray which i read is

                QByteArray arr = socket->read(4);
                // arr.at(0) = 0x41
                // arr.at(1) = 0xf8
                // arr.at(2) = 0xf6
                // arr.at(3) = 0x00
                

                When i convert the read QByteArray with the QDataStream i get zero (but thats not the value displayed on the device). So i convert the QByteArray with the online converter (https://www.h-schmidt.net/FloatConverter/IEEE754.html) and get the correct value. So i thought i do not use the QDataStream class correctly. But from the same device i get also values with double precesion (8 bytes). Converting these values with QDataStream is no problem, only float values

                1 Reply Last reply
                0
                • beeckscheB beecksche

                  Hello,
                  from a TCP Socket i get 4 bytes representing a float value in the IEEE 754 standard.

                  For example:

                  QByteArray arr = socket->read(4);
                  // arr.at(0) = 0x41
                  // arr.at(1) = 0xf8
                  // arr.at(2) = 0xf6
                  // arr.at(3) = 0x00
                  

                  Is there an easy way to convert it? For double values i use the QDataStream like

                  QByteArray arr = socket->read(8);
                  
                  double number;
                  QDataStream(arr);
                  stream >> number;
                  

                  But this won't work with float values.

                  I have also tried

                  bool ok = false;
                  float val = socket->read(4).toFloat(&ok);
                  // ok = false!
                  

                  Thanks you

                  S Offline
                  S Offline
                  Stoyan
                  wrote on last edited by
                  #8

                  @beecksche
                  You can use QDataStream for float too. Just have to set this option:

                  stream.setFloatingPointPrecision(QDataStream::SinglePrecision); // for float (4-bytes / 32-bits)
                  

                  or

                  stream.setFloatingPointPrecision(QDataStream::DoublePrecision); // for double (8-bytes / 64-bits)
                  

                  Default value is QDataStream::DoublePrecision. That's why in your case it works with double, and not with float.

                  beeckscheB 1 Reply Last reply
                  7
                  • S Stoyan

                    @beecksche
                    You can use QDataStream for float too. Just have to set this option:

                    stream.setFloatingPointPrecision(QDataStream::SinglePrecision); // for float (4-bytes / 32-bits)
                    

                    or

                    stream.setFloatingPointPrecision(QDataStream::DoublePrecision); // for double (8-bytes / 64-bits)
                    

                    Default value is QDataStream::DoublePrecision. That's why in your case it works with double, and not with float.

                    beeckscheB Offline
                    beeckscheB Offline
                    beecksche
                    wrote on last edited by
                    #9

                    @Stoyan
                    Thanks a lot. Works perfect now!

                    1 Reply Last reply
                    1

                    • Login

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