Thread: converting char to int

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    67

    converting char to int

    how can i convert a character to an integer?

    e.g. char a = '8'; then convert a to an integer, so it can then be converted to binary.

    i know atoi converts a string to an integer, but i cant seem to find anything to do it with chars.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Subtract '0'.
    Code:
    char a = '8';
    int i = a - '0';
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    24
    Code:
    int ctoi(char ch) {
     char temp[2] = {ch, 0};
     return atoi(temp);
     }
    you could use simple arithemetic on the character values too, but I'm not sure how portable that would be.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    More portable than the code you posted.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    24
    the encoding used may not yield the correct value using that method. for instance, using gray's code there's a single bit of difference between neighboring values - so subtracting values would probably result in an error. on the other hand, atoi() relies on the C locale mechanism for character conversion.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Okay, I am merely referring to the C language.
    the 10 decimal digits
    0 1 2 3 4 5 6 7 8 9
    [...]
    In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.
    And Gray code wouldn't work for C since C mandates a pure binary encoding.
    Last edited by Dave_Sinkula; 05-02-2005 at 01:39 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

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