Thread: Int to Char

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    12

    Int to Char

    I'm trying to convert integers to char. Is there anyway to do this. I'm coding a game and am using rand to generate a range of numbers. After 10 these numbers have to be represented by letters. Thanks for your help.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Yes. Presumably you are converting the integers 0-9 to the characters '0' - '9' and want 10, for example to become 'A'? If so you can use the fact that characters are actually stored as integer codes so...
    Code:
    char x;
    int i;
    
    i = 10;
    x = (char) i + ('A' - 10);
    ... will give A for 10, B for 11, C for 12 ... up to Z for 36. Beyond 36, you will get some other characters depending on the character set of you machine.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    you could use itoa,ltoa etc functions in stdlib.h
    -

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> use itoa,ltoa

    You have to be a bit careful here. Without knowing really what he is doing, (he didn't post his code!!!). itoa converts to a null terminated string, not a single character.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    uhh... sorry
    -

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> uhh... sorry

    Not necessary! That may well be what he wants, but without knowing what he is doing with his digits and letters, it is impossible to be sure!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. 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
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM