• Advertisement
Sign in to follow this  

COUTing a BYTE with Dev C++

This topic is 4945 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement