Results 1 to 20 of 20

Thread: Converting to negative int - bug

  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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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

  13. #13
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Converting to negative int - bug

    I don't understand why you are bothering with all these conversions. Most of them are wrong and make no sense in light of what you're trying to achieve. But more importantly, the data as it sits in memory is already in the form you want it in. If you receive an IEEE float value, it's ALREADY in floating point format, and all you have to do is print it out, no conversions necessary, although you may have to jigger pointer types to ensure that it is interpreted by the streams operators correctly. But trying to hammer it into a hex value, then into an int value and finally into a float value is pointless; all you're doing is messing up data that was perfectly good to begin with.

    The only issue that might arise is receiving data over a network with a machine whose endianess differs from network byte order. This sort of problem is unlikely - I would think the socket layer would take care of such things without your intervention - but if simply treating your value as a float directly doesn't work, try byte-swapping it and outputting those results.

    Right now, you're making the problem way too hard. In fact, you're assuming there's a problem where there really isn't one.

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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
    Even your example you suggested in your post returns 0
    No, it doesn't.

    Qt Code:
    1. #include <QtCore>
    2.  
    3. int main(int argc, char **argv){
    4. QByteArray ba("\xBD\xC6\xD6\x04", 4);
    5. float value;
    6. memcpy(&value, ba.constData(), sizeof(float));
    7. qDebug() << value;
    8. return 0;
    9. }
    To copy to clipboard, switch view to plain text mode 

    This yields 5.04937e-36.
    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.


  15. #15
    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

    Thanks for advice, but at the end of the day, I don't know how!
    The original issue came up when I was trying to do a MODBUS application, and I got it to work back then by doing what I'm doing now.

    Like so...?
    Qt Code:
    1. m_DataEccentricity[0] = block[21];
    2. m_DataEccentricity[1] = block[22];
    3. m_DataEccentricity[2] = block[23];
    4. m_DataEccentricity[3] = block[24];
    5. box.setText(m_DataEccentricity);
    6. box.exec();
    To copy to clipboard, switch view to plain text mode 

    The camera (where we are reading inputs from) has gone down, so no way to test it right now.

    Testing
    Qt Code:
    1. QByteArray ba("\xBD\xC6\xD6\x04");
    2. box.setText(ba);
    3. box.exec();
    To copy to clipboard, switch view to plain text mode 
    doesn't work though.
    Last edited by ShamusVW; 22nd September 2010 at 11:10. Reason: Addition

  16. #16
    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

    My apologies, you're right, it is 5.04937e-36 = 0.00...[x35]..504937, I had originally output it to a QMessageBox and rounded it to 6 figures, hence 0.
    But it still isn't -0.09708788990974426
    Last edited by ShamusVW; 22nd September 2010 at 11:21. Reason: Addition

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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

    Look, you can't blindly try different things without any knowledge on what you are doing. You have to read some documentation for the devices and software you are using. You have to know whether a device reports values in little endian or big endian, what is the size of its data type, etc. The same goes for the device where you accept those values. You can't just say "convert to IEEE-754" because the proper answer to this would be a question "convert from what?". Without knowing how your data is encoded there is no way of telling how to decode it.

    I gave you a code that reads 4 bytes of data and interprets it as a floating point value in the format default for the target platform. There is no conversion involved, the data is simply interpreted as a specific type. If by repeating my process you get "0" then your initial byte array must have contained the representation of "0". If you expected some other value (whatever the encoding) then your byte array was probably invalid in the first place. Trying to display the byte array on a message box is a bad idea - the value is binary and not textual, it contains non-printable characters.

    By the way, I doubt "float" can contain the value you want, it is a single precision floating point datatype.
    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.


  18. #18
    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 have values in my QByteArray. In each position I have 2 bytes as hex values. When I do the conversion manually (i.e. using a calculator) it gives the correct result, in the order that it is retrieved. However, this I did back then as a check. I then checked it on that website, and it gave correct results. I know about endianess (but for what I want to do, obviously in incorrect order!), and it is coming through in correct (Nope) order. When I run qDebug, I will get values like

    qDebug() << QString::number(block[9],16);
    qDebug() << QString::number(block[10],16);
    qDebug() << QString::number(block[11],16);
    qDebug() << QString::number(block[12],16);

    it prints out

    3d
    7e
    bf
    26

    Converting to floating point I get 0.062194012105464935 which is correct result using my method (albiet it maybe incorrect).
    It is fine for you to say not to blindly try things without knowledge etc, etc. I appreciate this. But at this point I've been busy trying to get this to work on negative numbers for a long time. If then it is said to not convert anything, all in correct way to start with, then fine, I can accept that, but then how exactly do I use it? Because I've most certainly tried a lot of different things to get it to work, no luck. The values I have are hex, how do I get them to floating point, that will include negatives. I also tried the memcpy suggestion, it doesn't "appear" to work in the sense that I am getting 0, so what am I doing wrong.

    I really do appreciate your help, and I don't want to have you stop it, understand I'm trying to keep up with your knowledge too.
    Last edited by ShamusVW; 22nd September 2010 at 12:03. Reason: Correction

  19. #19
    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

    Trying again...

    Qt Code:
    1. QByteArray ab("\x3D\x7E\xBF\x26",4);
    2. float value;
    3. memcpy(&value, ab.constData(), sizeof(float));
    4. qDebug() << value;
    To copy to clipboard, switch view to plain text mode 

    I get 1.32875e-15
    On the website, it converts to 0.062194012105464935

    HOWEVER... when I reverse it all as input to the website, i.e. 26BF7E3D, it returns 1.32875e-15

    This also works for negatives!
    So based on the way I was doing it (incorrectly) worked for positives, broke for negatives. I will have to reverse my positioning in my QByteArray and then the new method will work.

    Thank you.
    A very useful lesson
    Shaun

  20. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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
    I know about endianess, and it is coming through in correct order.
    Correct order for your calculations or for the destination platform?

    If then it is said to not convert anything, all in correct way to start with, then fine, I can accept that, but then how exactly do I use it?
    That's where the research part comes in. We are not going to do it for you.

    Because I've most certainly tried a lot of different things to get it to work, no luck.
    That's because you are counting on luck. Do it the scientific way instead. The first thing I would try would be to input the value you want to receive, dump its internal representation and compare it to your other representation.

    The values I have are hex, how do I get them to floating point,
    All values are hex. They are also oct, dec and binary. Hexadecimal only determines the base for encoding the value - your "floating point" is also "hex".

    that will include negatives.
    In what encoding? A binary (or hexadecimal, or whatever) value "becomes" negative only if you interpret it as such and again this depends on the encoding. 0x7F can be interpreted as "-1" but also as "+127". In the former encoding you could encode "+127" as 0x007F. In yet another encoding "-1" will be 0x80.

    it doesn't "appear" to work in the sense that I am getting 0, so what am I doing wrong.
    You are getting 0 because you lose precision while dumping the value. "0.1" cast to int will give "0".
    Last edited by wysota; 22nd September 2010 at 12:15.
    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.


  21. The following user says thank you to wysota for this useful post:

    ShamusVW (22nd September 2010)

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.