tags:

views:

1384

answers:

2

Hi, If I have a char which holds a hex value such has 0x53, (S), how can I display this as "S"?

Code:

char test = 0x53;
cout << test << endl;

Thanks!

+5  A: 

There's no such thing as a variable that stores a hex value, or a decimal or octal value. Hex, octal, and decimal are just different ways of representing numbers to the compiler. The compiled code will represent everything in binary.

These statements all have the exact same effect (assuming the charset is ASCII):

test = 0x53; // hex
test = 'S';  // literal constant
test = 83;   // decimal
test = 0123; // octal

So print the character the same way you would with any character, no matter how you assign it a value.

yjerem
Your first statement is a too strong. Bob's test does hold a hex value; that hex value is 0x53. Granted, it also holds decimal 83 and octal 0123, but it does hold 0x53 too.
Jonathan Leffler
+2  A: 

Just use the following, you have already answered your question:

using namespace std;

int main() {
    char test = 0x53;
    std::cout << test << std::endl;
    return 0;
}
gagneet
if you're calling the std namespace, why are you prefixing "cout" and "endl" with "std::"? seems a little redundant to me.
Josh Sandlin
sorry my mistake in using the std:: again after calling the std namespace. just a matter of habit :(
gagneet