Thread: from char to int

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    Cool from char to int

    Hi, peeps. I was in the middle of ceatinga saved game feature to my game, when I ran into a problem. Is there a way to read a char and translate it to an int. Example "1" = 1. Is there a way to do this without annoying checks and case statements?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    char c='1';
    int i = c-'0';
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    I don't get it.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Unregistered
    Guest
    __________________
    "If you tell the truth, you don't have to remember anything"

    -Mark Twain

    If you don't remember anything, you don't have to tell the truth

    -L. Obotomy

  5. #5
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    Subtract the ASCII value of zero from the ASCII value of the character. Since the chars are stored as ASCII codes anyway, just subtract '0'
    from your character to convert.

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