Results 1 to 4 of 4

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

  1. #1
    Join Date
    Apr 2010
    Posts
    22
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

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

    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
    Qt Code:
    1. QByteArray array="425AE78F";
    2. bool ok;
    3. qDebug() << array.toInt(&ok,16); //returns: 1113253775
    4. qDebug() << array.toFloat(&ok); //returns: 0
    To copy to clipboard, switch view to plain text mode 
    This didn't worked too.
    Qt Code:
    1. bool ok;
    2. QByteArray text = QByteArray::fromHex("425AE78F");
    3. qDebug() << text.data(); //returns: BZç
    4. qDebug() << text.toFloat(&ok); //returns: 0
    To copy to clipboard, switch view to plain text mode 

    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?
    Last edited by Tadas; 18th April 2010 at 16:00.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How to correctly use this function: float QByteArray::toFloat ( bool * ok = 0 ) c

    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:
    Qt Code:
    1. bool ok;
    2. int sign = 1;
    3. QByteArray array("425AE78F");
    4. array = QByteArray::number(array.toLongLong(&ok,16),2); //convert hex to binary -you don't need this since your incoming data is binary
    5. if(array.length()==32) {
    6. if(array.at(0)=='1') sign =-1; // if bit 0 is 1 number is negative
    7. array.remove(0,1); // remove sign bit
    8. }
    9. QByteArray fraction =array.right(23); //get the fractional part
    10. double mantissa = 0;
    11. for(int i=0;i<fraction.length();i++) // iterate through the array to claculate the fraction as a decimal.
    12. if(fraction.at(i)=='1') mantissa += 1.0/(pow(2,i+1));
    13. int exponent = array.left(array.length()-23).toLongLong(&ok,2)-127; //claculate the exponent
    14. qDebug() << "number= "<< QString::number( sign*pow(2,exponent)*(mantissa+1.0),'f', 5 );
    15.  
    16. // output is- number= 54.72613
    To copy to clipboard, switch view to plain text mode 

    Hope this helps.
    Last edited by norobro; 19th April 2010 at 04:49. Reason: code correction

  3. The following user says thank you to norobro for this useful post:

    Tadas (20th April 2010)

  4. #3
    Join Date
    Apr 2010
    Posts
    22
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: How to correctly use this function: float QByteArray::toFloat ( bool * ok = 0 ) c

    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

    Qt Code:
    1. array = QByteArray::number(array.toLongLong(&ok,16),2);
    To copy to clipboard, switch view to plain text mode 

    So thank you again.

  5. #4

    Default Re: How to correctly use this function: float QByteArray::toFloat ( bool * ok = 0 ) c

    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:

    Qt Code:
    1. QFile infile("inputfile.bin");
    2. infile.open(QIODevice::ReadOnly);
    3. QByteArray buffer = infile.readAll();
    4. QByteArray array = buffer.mid(8, 4);
    5.  
    6. // The following code is the same as the example
    7. bool ok;
    8. int sign = 1;
    9. array = QByteArray::number(array.toLongLong(&ok,16),2);
    10. ...
    11. ...
    To copy to clipboard, switch view to plain text mode 

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

    Help! Thanks!
    Last edited by frustrated; 18th November 2010 at 18:07.

Similar Threads

  1. Replies: 6
    Last Post: 24th January 2011, 18:48
  2. Replies: 1
    Last Post: 10th February 2009, 09:42
  3. emitting signals in const function
    By maxel in forum Qt Programming
    Replies: 3
    Last Post: 19th October 2008, 15:05
  4. subclassed const function problem
    By qtneuling in forum Newbie
    Replies: 8
    Last Post: 22nd June 2008, 02:52
  5. const function parameter problems
    By stevey in forum General Programming
    Replies: 3
    Last Post: 18th December 2006, 22:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.