COUTing a BYTE with Dev C++
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
typedef unsigned char BYTE;
that should do it.
you might want to make a header file of your own of the typedefs you want.
that should do it.
you might want to make a header file of your own of the typedefs you want.
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
D
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;
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;
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;
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
Guess I will just have to do the cast.
darryl
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement