Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   How to convert a number between 0-9 to Char (http://www.go4expert.com/forums/convert-0-9-char-t12096/)

Musab 14Jul2008 18:54

How to convert a number between 0-9 to Char
 
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:

shabbir 14Jul2008 19:18

Re: How to convert a number between 0-9 to Char
 
Directly assigning should do the job for you.

char c = i;

akhanna01 14Jul2008 19:33

Re: How to convert a number between 0-9 to Char
 
add the ascii value to it................

xpi0t0s 16Jul2008 14:23

Re: How to convert a number between 0-9 to Char
 
if (i>=0 && i<=9) c=i+'0';
Or just generate random numbers between '0' and '9'.

Musab 17Jul2008 07:48

Re: How to convert a number between 0-9 to Char
 
Thanks for the tip...yes rand()%10 + '0' works

aali 20Jul2008 13:53

Re: How to convert a number between 0-9 to Char
 
use dynamic cast
or
for example
int a;
char b;
b<int>=a;

xpi0t0s 20Jul2008 14:43

Re: How to convert a number between 0-9 to Char
 
yes, but will that convert 0x00000000 to '0', which is what the OP wanted, or will it convert it to 0x00?


All times are GMT +5.5. The time now is 15:22.