COUTing a BYTE with Dev C++

Started by
5 comments, last by Fruny 19 years, 9 months ago
Hi, For the given example below: #include <iostream> using namespace std; int main() { BYTE number; number = 65; cout << number << endl; } MSVC will output 65 as expected (my expectations anyway) while Dev C++ will output 'A' How can I get the first behavior using Dev C++? I don't want to use an int for number because in my real program I perfom some bitwise computations which will be wrong using a 32 bit int but works with an 8 bit BYTE. Thanks, Darryl
Anti-Sig: Do Not Read This Signature
Advertisement
typedef unsigned char BYTE;

that should do it.

you might want to make a header file of your own of the typedefs you want.

Beginner in Game Development?  Read here. And read here.

 

Still outputs 'A' and not 65. BTW I looked through the headers and found that's the way it is already defined as an unsigned char. I imagine the problem is with iostream cout treating it as a char.

D
Anti-Sig: Do Not Read This Signature
Quote:Original post by darrylsh
I imagine the problem is with iostream cout treating it as a char.


Well, DUH! An unsigned char (BYTE is just a typedef, it's not something that exists on its own) is a character. You know, a letter. Cast to unsigned int if you want a numerical value.

cout << static_cast<unsigned int>(number) << endl;
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
i should learn how to read.... [rolleyes]

if BYTE is an unsigned char then it makes sense that COUTting 65 would produce 'A'. 65 is the ASCII value for 'A'. try to cast it with (int).

so... cout << (int) number << endl;

Beginner in Game Development?  Read here. And read here.

 

I hear what you guys are saying, HOWEVER, using MSVC cout << BYTE outputs the number and not the character and I was hoping to somehow duplicate that behavior without cast. Basically how is MS defining BYTE or cout such that cout << BYTE outputs the value and not the character.

Guess I will just have to do the cast.

darryl
Anti-Sig: Do Not Read This Signature
Quote:Original post by darrylsh
I hear what you guys are saying, HOWEVER, using MSVC cout << BYTE outputs the number and not the character and I was hoping to somehow duplicate that behavior without cast.


If so, MSVC's behaviour is incorrect. If you are using VC6, it is not surprising.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement