Results 1 to 20 of 20

Thread: Converting to negative int - bug

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Converting to negative int - bug

    Hello
    I am trying to use QString::toInt and have run into an issue that negative numbers get returned as zero.
    Googling, I found http://bugreports.qt.nokia.com/brows...3Aall-tabpanel.

    Can anyone help me get round this issue?

    I have in my code
    Qt Code:
    1. fl = QString(m_DataYPoint.toHex()); //convert the data to string
    2. intValue = fl.toInt(0,16); //...and the string to an int
    3. m_YPoint = *(float*)&intValue;
    To copy to clipboard, switch view to plain text mode 

    where
    m_DataYPoint is of type QByteArray
    intValue of type qint32
    m_YPoint of type float.

    Thanks,
    Shaun

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Converting to negative int - bug

    So where is the negative value and why are you converting an integer to a float in such a weird and incorrect way?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Converting to negative int - bug

    I am reading data from a socket stored in m_DataYPoint (QByteArray).
    I want to convert it to floating point, and until I realised that I also have negative possibilities, it worked fine.

    As an example, m_DataYPoint might contain "BDC6D604". This should equal "-0.09708..." but it gets returned as 0.

    ...what is the correct way...?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Converting to negative int - bug

    Quote Originally Posted by ShamusVW View Post
    As an example, m_DataYPoint might contain "BDC6D604". This should equal "-0.09708..." but it gets returned as 0.
    If BDC6D604 is not an integer then why are you converting it to an integer?

    ...what is the correct way...?
    A correct way to cast an int to float is:
    Qt Code:
    1. int a = 7;
    2. float b = a;
    To copy to clipboard, switch view to plain text mode 
    Using pointers will cast pointers and make the internal representation of the integer be interpreted as an internal representation of a floating point value.

    Anywho... If you read a float from the socket then first you have to ask the person who sent it what kind of encoding it uses (different platforms may have different encodings of floats or ints). Assuming it is the same as on your destination platform, simply do:
    Qt Code:
    1. QByteArray ba("\xBD\xC6\xD6\x04",4);
    2. float value;
    3. memcpy(&value, ba.constData(), sizeof(float));
    To copy to clipboard, switch view to plain text mode 

    By the way, 0xBDC6D604 is 3,183,924,740 decimal (won't fit into 32 bit signed integer, so no way you would convert it through an int typed variable).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default

    I'm using IEEE-754 floating point.
    Thanks, I will test it out.
    Thanks for your input.

    See http://babbage.cs.qc.edu/IEEE-754/32bit.html for correct conversion of floating point.
    Last edited by wysota; 21st September 2010 at 16:07.

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Converting to negative int - bug

    That is something completely different than a int.

    Look at documentation of IEEE-754 and the source code of that website to see how to convert it correctly.

    toInt() certainly is not correct.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Converting to negative int - bug

    Just don't do the conversion (if you need any conversion at all!) by going through strings. If IEEE-754 is the officially used floating point representation then the code I gave you should work.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Converting to negative int - bug

    Using pointers will cast pointers and make the internal representation of the integer be interpreted as an internal representation of a floating point value.
    True - this is what I'm wanting, just not sure why negative is not working.
    I have searched for ages looking for an efficient way to convert hex to floating point. When I originally implemented this solution, I'm not sure how I came upon it, but it worked. Unfortunately I was then only working in positive values.

    Look at documentation of IEEE-754 and the source code of that website to see how to convert it correctly.
    Ok, found the source! There seriously is not a shorter way to convert hex to float?

    Qt Code:
    1. QByteArray ba("\xBD\xC6\xD6\x04",4);
    2. float value;
    3. memcpy(&value, ba.constData(), sizeof(float));
    To copy to clipboard, switch view to plain text mode 
    returns 0 again!

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Converting to negative int - bug

    What is the contents of your byte array? Dump it to qDebug() please.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Converting to negative int - bug

    For a background of my application, see http://www.qtcentre.org/threads/2509...ht=#post121381

    I have a function
    Qt Code:
    1. void Client::readSocket()
    2. {
    3. QByteArray block;
    4. block = tcpSocket->readAll();
    5. m_DataEccentricity[0] = block[9];
    6. m_DataEccentricity[1] = block[10];
    7. m_DataEccentricity[2] = block[11];
    8. m_DataEccentricity[3] = block[12];
    9. convertToFloat();
    10. ...
    11. }
    To copy to clipboard, switch view to plain text mode 

    where
    Qt Code:
    1. QByteArray m_DataEccentricity;
    To copy to clipboard, switch view to plain text mode 

    Convert to float...
    Qt Code:
    1. void Client::convertToFloat()
    2. {
    3. QString fl;
    4. fl = QString(m_DataEccentricity.toHex()); //convert the data to string
    5. qint32 intValue = fl.toInt(0,16); //...and the string to an int
    6. m_Eccentricity = *(float*)&intValue;
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 

    and in another function where I do updating of the GUI

    Qt Code:
    1. eccentricityLineEdit->setText(QString::number(m_Eccentricity,'f',6));
    To copy to clipboard, switch view to plain text mode 

    With regard to contents of my byte array, doing a
    Qt Code:
    1. qDebug (DataEccentricity)
    To copy to clipboard, switch view to plain text mode 
    gives "¾ _h", but that isn't a blank between 3/4 and _, it is supposed to be a vertical bar with a small bar midway up pointing to the right (like an "H" without the right bar)
    Last edited by ShamusVW; 22nd September 2010 at 09:24. Reason: Correction

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Converting to negative int - bug

    And using my code on it gives you 0?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Converting to negative int - bug

    Yes.
    Even your example you suggested in your post returns 0

    Qt Code:
    1. QByteArray ba("\xBD\xC6\xD6\x04",4);
    2. float value;
    3. memcpy(&value, ba.constData(), sizeof(float));
    To copy to clipboard, switch view to plain text mode 

    ...but I would like result of -0.09708788990974426 (as per IEEE-754 spec)
    Last edited by ShamusVW; 22nd September 2010 at 09:58. Reason: Addition

Similar Threads

  1. QFontMetrics::leading() returns negative value.
    By JantarManter in forum Qt Programming
    Replies: 0
    Last Post: 19th May 2010, 14:45
  2. Replies: 1
    Last Post: 6th May 2010, 11:36
  3. How to zoom in negative X and Y axis ?
    By biplab777 in forum Qwt
    Replies: 0
    Last Post: 9th June 2009, 10:55
  4. color image to negative
    By sm in forum Newbie
    Replies: 1
    Last Post: 29th January 2008, 15:25
  5. negative images
    By tommy in forum Qt Programming
    Replies: 3
    Last Post: 22nd January 2008, 10:41

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
  •  
Qt is a trademark of The Qt Company.