Thread: Converting an int into a char

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    8

    Converting an int into a char

    Good day!
    I have this problem: I have a number (int) and I want to know who's simbol's ASCII code is.

    From letter to number works well:
    Code:
    char s[10];
    int i;
    for(i=0;i<strlen(s);i++)printf("%d",s[i]);
    How should I find the simbol after my int?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Assuming it is a single digit number, add it to '0'. The digits '0' through '9' are guaranteed to be in order. That will give you a character and you can get the value of the symbol from that in the same way you did above.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I have this problem: I have a number (int) and I want to know who's simbol's ASCII code is.
    So you have a number, say 65, and you want to see what character that represents? Just print it with %c:
    Code:
    printf("%c", 65);
    or putchar():
    Code:
    putchar(65);
    You might want to print a '-' or something if the character isn't printable:
    Code:
    #include <ctype.h>  /* for isprint() */
    
    if(isprint(c)) putchar(c);
    else putchar('-');
    Some characters can't be printed, or won't show up properly if they are.

    BTW, this is the C++ forum, not the C one.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    That realy helped!
    10x

    P.s.: next time I'll post on the C forum. 10x again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM