Istringstream gives me wrong result for a signed short int


I have :
signed short int my_i // -32768 to 32767

std::istringstream i(value);
i>>my_i

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 ?
Thanks
Last edited on
This is correct: 38000 wraps around (due to sign extension) to -27536 when you try to stuff it into a signed short.
Topic archived. No new replies allowed.