How to output a character as an integer through cout?

C++IoType ConversionIostreamOutputstream

C++ Problem Overview


#include <iostream>

using namespace std;

int main()
{  
    char          c1 = 0xab;
    signed char   c2 = 0xcd;
    unsigned char c3 = 0xef;
    
    cout << hex;
    cout << c1 << endl;
    cout << c2 << endl;
    cout << c3 << endl;
}

I expected the output are as follows:

ab
cd
ef

Yet, I got nothing.

I guess this is because cout always treats 'char', 'signed char', and 'unsigned char' as characters rather than 8-bit integers. However, 'char', 'signed char', and 'unsigned char' are all integral types.

So my question is: How to output a character as an integer through cout?

PS: static_cast(...) is ugly and needs more work to trim extra bits.

C++ Solutions


Solution 1 - C++

char a = 0xab;
cout << +a; // promotes a to a type printable as a number, regardless of type.

This works as long as the type provides a unary + operator with ordinary semantics. If you are defining a class that represents a number, to provide a unary + operator with canonical semantics, create an operator+() that simply returns *this either by value or by reference-to-const.

source: Parashift.com - How can I print a char as a number? How can I print a char* so the output shows the pointer's numeric value?

Solution 2 - C++

Cast them to an integer type, (and bitmask appropriately!) i.e.:

#include <iostream>

using namespace std;

int main()
{  
    char          c1 = 0xab;
    signed char   c2 = 0xcd;
    unsigned char c3 = 0xef;

    cout << hex;
    cout << (static_cast<int>(c1) & 0xFF) << endl;
    cout << (static_cast<int>(c2) & 0xFF) << endl;
    cout << (static_cast<unsigned int>(c3) & 0xFF) << endl;
}

Solution 3 - C++

Maybe this:

char c = 0xab;
std::cout << (int)c;

Hope it helps.

Solution 4 - C++

What about:

char c1 = 0xab;
std::cout << int{ c1 } << std::endl;

It's concise and safe, and produces the same machine code as other methods.

Solution 5 - C++

Another way is to overload the << operator:

#include <iostream>

using namespace std;
typedef basic_ostream<char, char_traits<char>> basicOstream;

/*inline*/ basicOstream &operator<<(basicOstream &stream, char c) {
	return stream.operator<<(+c);
}
/*inline*/ basicOstream &operator<<(basicOstream &stream, signed char c) {
	return stream.operator<<(+c);
}
/*inline*/ basicOstream &operator<<(basicOstream &stream, unsigned char c) {
	return stream.operator<<(+c);
}

int main() {
	char          var1 = 10;
	signed char   var2 = 11;
	unsigned char var3 = 12;
	
	cout << var1 << endl;
	cout << var2 << endl;
	cout << var3 << endl;
	
	return 0;
}

which prints the following output:

10
11
12

Process finished with exit code 0

I think it's very neat and useful. hope it hepls!


And Also if you want it to print a hex value you can do like this:

basicOstream &operator<<(basicOstream &stream, char c) {
	return stream.operator<<(hex).operator<<(c);
} // and so on...

Solution 6 - C++

Another way to do it is with std::hex apart from casting (int):

std::cout << std::hex << (int)myVar << std::endl;

I hope it helps.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionxmllmxView Question on Stackoverflow
Solution 1 - C++danielView Answer on Stackoverflow
Solution 2 - C++sheuView Answer on Stackoverflow
Solution 3 - C++Luka PivkView Answer on Stackoverflow
Solution 4 - C++mt3dView Answer on Stackoverflow
Solution 5 - C++MMDView Answer on Stackoverflow
Solution 6 - C++omottoView Answer on Stackoverflow