Eric wrote in message ...
Is there a simple, standard statement that will reinterpret an int in
the range of 0-9 as a char? I understand it's simple enough to write in
a function, but I wonder if there's a more general approach.
In case the other answers were not what you wanted:
int num( 9 );
char cnum( num );
or insure the int will fit in a char:
num &= 0x7F;
char cnum2( num );
or if you need to assign:
cnum = static_cast<char>( num );
Be aware that 'char' may be 'unsigned char' or 'signed char' depending on
implementation/OS.
You can check which is used (among other ways):
#include <limits>
{
std::cout <<"numeric_limits<char>::max() ="
<<int(std::numeric_limits<char>::max())<<std::endl;
std::cout <<"numeric_limits<char>::min() ="
<<int(std::numeric_limits<char>::min())<<std::endl;
std::cout <<"numeric_limits<unsigned char>::max() ="
<<int(std::numeric_limits<unsigned char>::max())<<std::endl;
std::cout <<"numeric_limits<unsigned char>::min() ="
<<int(std::numeric_limits<unsigned char>::min())<<std::endl;
}
If the outputs match, 'char' == 'unsigned char'.
[ if you knew all this already, sorry. Maybe it might help some newbie.]