# How to convert a number between 0-9 to Char

Discussion in 'C' started by Musab, Jul 14, 2008.

1. ### MusabNew Member

Joined:
Jul 1, 2008
Messages:
7
0
Trophy Points:
0
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 :sosp:

Joined:
Jul 12, 2004
Messages:
15,285
364
Trophy Points:
83
Directly assigning should do the job for you.

char c = i;

3. ### akhanna01New Member

Joined:
Jul 12, 2008
Messages:
5
0
Trophy Points:
0
add the ascii value to it................

4. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
if (i>=0 && i<=9) c=i+'0';
Or just generate random numbers between '0' and '9'.

5. ### MusabNew Member

Joined:
Jul 1, 2008
Messages:
7
0
Trophy Points:
0
Thanks for the tip...yes rand()%10 + '0' works

6. ### aaliNew Member

Joined:
Jul 16, 2008
Messages:
18
0
Trophy Points:
0
Occupation:
student
Location:
use dynamic cast
or
for example
int a;
char b;
b<int>=a;

Joined:
Aug 6, 2004
Messages:
3,012