Results 1 to 4 of 4

Thread: Istringstream gives me wrong result for a signed short int

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Istringstream gives me wrong result for a signed short int

    I have :
    Qt Code:
    1. signed short int my_i // -32768 to 32767
    2.  
    3. std::istringstream i(value);
    4. i>>my_i
    To copy to clipboard, switch view to plain text mode 
    Ok, if I pass value "38000" my_i gives me -27536
    Ok, if I pass value "-27536" my_i gives me -27536

    So the instruction to know if can be posible to convert to My_i does not fails.
    if (!(i >> My_i) )
    for the value "38000" 'i' have not to convert, it should to fail, but it works ... converting 38000 to -27536 )


    In this situation I can't have the correct value.
    Any help ? Maybe mingw the guilty ?
    Thanks

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Istringstream gives me wrong result for a signed short int

    I don't see why the code i>>my_i would ever return zero?

  3. #3
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Istringstream gives me wrong result for a signed short int

    If I have for std::istringstream i(value); a value of 450000
    (i >> My_i) are going to be false, because istringstream cannot 'send' a long value to a signed short int, isn't ?
    Ok, then why Is the reason because 38000 , value that also would have to be converted to long, is converted to -27536 ?
    Thanks


    Added after 4 minutes:


    Uf , delete this post please
    I have mixed int and short int inside functions ....
    Last edited by tonnot; 13th May 2011 at 20:43.

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

    Default Re: Istringstream gives me wrong result for a signed short int

    You don't understand how stringstreams work. Stop making assumptions and go read some documentation.

    And your test is silly to begin with. You can't stuff an 'illegal' value into a variable that's been declared to be of a particular type already; whatever value it holds will ALWAYS be legitimate and within the range of values allowed by the type. There's no point in performing the check you're attempting in the way you're trying to go about it.

Similar Threads

  1. Symbian - self-signed certificate
    By metRo_ in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 10th March 2010, 06:10
  2. Static casting of signed chars
    By ShamusVW in forum Qt Programming
    Replies: 0
    Last Post: 4th November 2009, 05:46
  3. Amazon Product Advertising API - Signed requests
    By youkai in forum General Programming
    Replies: 2
    Last Post: 13th July 2009, 11:39
  4. Using Short cut keys
    By joseph in forum Qt Programming
    Replies: 14
    Last Post: 18th March 2008, 19:34
  5. Wrong compiling result by QT!!??
    By greenoaktree in forum Qt Programming
    Replies: 2
    Last Post: 13th January 2008, 13:36

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.