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;
array
= QByteArray::number(array.
toLongLong(&ok,
16),
2);
//convert hex to binary -you don't need this since your incoming data is binaryif(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
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
To copy to clipboard, switch view to plain text mode
Hope this helps.
Bookmarks