Int to Char

This is a discussion on Int to Char within the C++ Programming forums, part of the General Programming Boards category; I'm trying to convert integers to char. Is there anyway to do this. I'm coding a game and am using ...

  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,831
    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,831
    >>> 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,831
    >>> 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, 04: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, 09:36 PM
  3. Replies: 14
    Last Post: 06-28-2006, 02: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, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21