PDA

View Full Version : How to correctly use this function: float QByteArray::toFloat ( bool * ok = 0 ) const



Tadas
18th April 2010, 15:51
Hello,

I have tried to convert HEX value to float in many ways, but without any success.

I have QByteArray Hex value "425AE78F"
according to this site http://www.h-schmidt.net/FloatApplet/IEEE754.html answer should be: 54.72613

I have managed to convert this value only to DEC


QByteArray array="425AE78F";
bool ok;
qDebug() << array.toInt(&ok,16); //returns: 1113253775
qDebug() << array.toFloat(&ok); //returns: 0

This didn't worked too.

bool ok;
QByteArray text = QByteArray::fromHex("425AE78F");
qDebug() << text.data(); //returns: BZç
qDebug() << text.toFloat(&ok); //returns: 0

I have read about QByteArray, Qstring, but didn't find the answer, I'm new to QT and byte programming. I have created server with QTCpserver and QTcpsocket, I receive binary string from device to my computer. I convert binary string to Hex using toHex() function. Now i want to decode this Hex string. "425AE78F" - represents GPS coordinate Latitude and the answer should be 54.72613.

Can you tell me, what I'm doing wrong?

norobro
19th April 2010, 04:34
I may be mistaken but I don't think Qt provides functions for converting binary floating point numbers.

Using QByteArray to manipulate the data, it is rather trivial to implement the IEEE 754 formula on the page that you linked though:

bool ok;
int sign = 1;
QByteArray array("425AE78F");
array = QByteArray::number(array.toLongLong(&ok,16),2); //convert hex to binary -you don't need this since your incoming data is binary
if(array.length()==32) {
if(array.at(0)=='1') sign =-1; // if bit 0 is 1 number is negative
array.remove(0,1); // remove sign bit
}
QByteArray fraction =array.right(23); //get the fractional part
double mantissa = 0;
for(int i=0;i<fraction.length();i++) // iterate through the array to claculate the fraction as a decimal.
if(fraction.at(i)=='1') mantissa += 1.0/(pow(2,i+1));
int exponent = array.left(array.length()-23).toLongLong(&ok,2)-127; //claculate the exponent
qDebug() << "number= "<< QString::number( sign*pow(2,exponent)*(mantissa+1.0),'f', 5 );

// output is- number= 54.72613

Hope this helps.

Tadas
20th April 2010, 21:13
Thank you very much for help,
You have also answered my next question, how to convert HEX to binary :)
In documentation I was able to find about, how to convert binary to HEX, using function toHex()
But I could not find something like toBinary(). But this line makes everything clear


array = QByteArray::number(array.toLongLong(&ok,16),2);

So thank you again.

frustrated
18th November 2010, 17:52
Based on this example, I have a random binary file, I want to extract byte 8 to 11 from this file and decode these 4 bytes as a float. I have tried the following code, doesn't work:



QFile infile("inputfile.bin");
infile.open(QIODevice::ReadOnly);
QByteArray buffer = infile.readAll();
QByteArray array = buffer.mid(8, 4);

// The following code is the same as the example
bool ok;
int sign = 1;
array = QByteArray::number(array.toLongLong(&ok,16),2);
...
...


It seems that I cannot correctly extract the four bytes from the binary file.

Help! Thanks!