1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

How to convert a number between 0-9 to Char

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

  1. Musab

    Musab New Member

    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:
     
  2. shabbir

    shabbir Administrator Staff Member

    Directly assigning should do the job for you.

    char c = i;
     
  3. akhanna01

    akhanna01 New Member

    add the ascii value to it................
     
  4. xpi0t0s

    xpi0t0s Mentor

    if (i>=0 && i<=9) c=i+'0';
    Or just generate random numbers between '0' and '9'.
     
  5. Musab

    Musab New Member

    Thanks for the tip...yes rand()%10 + '0' works
     
  6. aali

    aali New Member

    use dynamic cast
    or
    for example
    int a;
    char b;
    b<int>=a;
     
  7. xpi0t0s

    xpi0t0s Mentor

    yes, but will that convert 0x00000000 to '0', which is what the OP wanted, or will it convert it to 0x00?
     

Share This Page