[Bug c++/23757] New: iostreams hex formatting for signed integers treats than as unsigned
x at xman dot org
gcc-bugzilla@gcc.gnu.org
Tue Sep 6 23:49:00 GMT 2005
I will attach the test.ii file later, but here's the sample source code:
#include <iostream>
using namespace std;
int main()
{
cout << dec << -1 << endl;
cout << hex << -1 << endl;
return 0;
}
The expected output is:
-1
-1
The actual output is:
-1
ffffffff
I found the problem in local_facets.tcc in the version of __int_to_char around
line 900. This is the one that takes a flag indicating if the integer is
negative or not. While the decimal case uses modular arithmetic and actually
checks the negative flag, the octal and hex cases use bitwise operators and
never check the negative flag. The octal and hex cases clearly assume they are
working with an unsigned integer, even though the templated types for the
function clearly allow for the case of a signed integer.
My current work around is to do the two's compliments math to convert the number
to it's postitive unsigned equivalent and then print out the "-" sign myself.
Something like:
cout << hex << (static_cast<uint16_t>(~aSession.getCount())+1) << endl;
--
Summary: iostreams hex formatting for signed integers treats than
as unsigned
Product: gcc
Version: 3.4.3
Status: UNCONFIRMED
Severity: minor
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: x at xman dot org
CC: gcc-bugs at gcc dot gnu dot org
GCC build triplet: i386-unknown-linux
GCC host triplet: i386-unknown-linux
GCC target triplet: i386-unknown-linux
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23757
More information about the Gcc-bugs
mailing list