Convert float to QbyteArray



  • Hi everyone, if I have a float variable like follows,how do i change it to QbyteArray ??Thanks in advance.
    @
    float data = 3.123;
    @



  • One way would be to use a QDataStream:

    @
    QByteArray result;
    QDataStream s(&result);
    s << data;
    @



  • Thanks. But it doesn't work....



  • What are you expecting exactly then? What should be the contents of your QByteArray exactly?



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



  • Did you spot QDataStream::setFloatingPointPrecision?



  • 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



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



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



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


  • Moderators

    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.



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



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


  • Moderators

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



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



  • 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: ).


  • Moderators

    [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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.