tags:

views:

84

answers:

2

Hi,

I've declared a uint8 variable and when the value in it is printed, I get smiley faces and white spaces. Shouldn't it display integer values?

+3  A: 

I bet uint8 is a typedef for unsigned char in your system headers. Then std::cout << u will print symbols rather than integer values, where u is of type uint8. Try

std::cout << static_cast< int >( u );

or

std::cout << +u;

to have numeric values printed.

usta
but wont that cast to int32?
Rajesh
Yes, it will. But what's the problem? That creates a temporary int from your uint8 variable and feeds that int to std::cout for printing.
usta
@Rajesh. If you are printing in decimal do you think it makes any difference if the value is stored in a uint8 or a uint32 just before it is printed?
Martin York
@usta: +1 for the nice little trick with the `+u` for the auto conversion to int.
Martin York
Thanks Martin :)
usta
I'm actually not printing directly, I'm reading it into a stringstream and then printing .str()
Rajesh
The same answer still applies. Instead of std::cout, you'll have some 'os' object of type std::stringstream
usta
+1 for the +u trick, very cool!
Sam Miller
Thank you Sam :)
usta
A: 

It all depends on how you printing the value out. For cout, try cout << (long unsigned int)var;, and for printf, you can try using the %lu format specifier. However, like usta mentioned, uint8 is likely defined or typedef'd as something else, since that is not an ISO C++ type. There is also the possibility that uint8 is typedef'd to unsigned long int (generic), or unsigned __int64 (MS platform specific), and the MSVC++ compiler is just doing the wrong implicit conversion. Without knowing what uint8 is defined as, it is hard to tell.

Javert93
uint8 is being used directly.... > typedef uint8 _UINT1; In the comment, it says 1 byte integer(How do i put a grey block around code?)
Rajesh
Well, then `_UIN1;` is likely defined as something else, too. You'd have to follow the typedef's until you hit the underlying intrinsic type (although, `_UINT1` seems to imply a single unsigned char, not a 64 bit number). Also, you can specify text as code (i.e. the "grey box") by wrapping it in back quote characters.
Javert93
typedef uint8 _UINT1; defines _UINT1 as alias to uint8, not the other way around. So you'll have to look for a typedef XXX uint8; declaration to find out what uint8 is. Or you can just trust me that it is an 8-bit unsigned integer, and is an alias for unsigned char :)[u]intN[_t] types always signify N-bit integers, not N-byte integers.
usta
Yep, my bad... I read it like it was a `#define` for some reason ::duh::. It also seems (like usta says), the 8 is referring to the number of bits in the number, not the number of bytes.
Javert93