Convert float to QbyteArray
-
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. -
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.
-
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));
@ -
Method 3:
@
float data = 3.54;
QString temp = QString::number(data);
QByteArray result = temp.data();
@ -
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 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-formatsIt describes possible issues when sending float data as raw bytes between different platforms. Your master and slave might not implement float the same way.