I have C++ program that reads from a binary file byte by byte then outputs it as a character to the console.
After using unsigned char
as a type, the console will print it's ascii value, which I didn't expect.
How do I get it to print the character instead of it's ascii value?
The reproducible code:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
unsigned char a;
a = 'A';
cout << (a > 31 ? a : '.');
}
The latest problem is caused by the mix of types in the conditional expression.
unsigned char a;
...
cout << (a > 31 ? a : '.');
This expression has type int because neither char type is big enough to include all the values of the other. (Apologies if the previous statement isn't totally accurate, I can't be bothered to read the fine print.)
Simply cast one char so that both result types for the conditional expression are the same, e.g. a > 31 ? a : (unsigned char)'.'
.