Qt World Summit: Register Today!

QByteArray to float value

  • 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;
    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

  • This post is deleted!

  • it might be horribly wrong but did you try:

    QByteArray arr = socket->read(4);
    float number =  *(reinterpret_cast<const float*>(arr.constData()));

  • Lifetime Qt Champion

    Are you sure the input is correct?
    ToFloat should work

    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 ?

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

    QByteArray arr;
    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.

  • Lifetime Qt Champion

    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?

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

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

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


    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.

  • @Stoyan
    Thanks a lot. Works perfect now!

Log in to reply