Decimal to ASCII char

Jul 8, 2012 at 4:23am
Is there any way to convert an int value to a char value that is corresponding to its ASCII value?
ex.
int a
a = 87
ASCII of a = 'W'

Thanks
Jul 8, 2012 at 7:01am
You can simply cast it.

1
2
int a = 87;
char W = static_cast<char>(a);
Jul 8, 2012 at 8:00am
You need not fo any conversion because int and char are integral types. It is only visual representation of a character provided by output functions for objects of type char.

So you can write simply

int a = 87;
char b = a;

or

std:;cout << ( char )a << std::endl;
or
std:;cout << static_cast<char>( a ) << std::endl;
or
std:;cout << b << std::endl;
Jul 11, 2012 at 5:03am
Thanks for the help the "static_cast<char>" works!
Jul 11, 2012 at 5:27am
There is
char itoa(int )
and
int atoi(char)
function also available.
Last edited on Jul 11, 2012 at 5:27am
Topic archived. No new replies allowed.