How to convert a number between 0-9 to Char

Musab's Avatar
Light Poster
Hello again

I'm generating a random number between 0 and 9
I want to assign that number to a character so I can store it in a character array;

something like
if i = 3
c = '3';

if i =4
c = '4'

but smarter way
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Directly assigning should do the job for you.

char c = i;
akhanna01's Avatar
Light Poster
add the ascii value to it................
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
if (i>=0 && i<=9) c=i+'0';
Or just generate random numbers between '0' and '9'.
Musab's Avatar
Light Poster
Thanks for the tip...yes rand()%10 + '0' works
aali's Avatar, Join Date: Jul 2008
Go4Expert Member
use dynamic cast
or
for example
int a;
char b;
b<int>=a;
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
yes, but will that convert 0x00000000 to '0', which is what the OP wanted, or will it convert it to 0x00?