C ++ ASCII value output

If I have a char that contains a hexadecimal value such as 0x53, (S), how can I display that as "S"?

code:

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

      

Thank!

-2


source to share


2 answers


There is no such thing as a variable that stores a hexadecimal value or 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.

All of these statements have the exact same effect (assuming ASCII encoding):



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

      

So, print a character just like you would any character, no matter how you assign a value to it.

+5


source


Just use the following, you already answered your question:



using namespace std;

int main() {
    char test = 0x53;
    std::cout << test << std::endl;
    return 0;
}

      

+1


source







All Articles