Thread: character code value???

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    10

    character code value???

    why would i subtract from 48 to convert to the character code values

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    "Characters" are nothing more than integers. Different integers are used to represent different characters. The ASCII encoding uses 48 to represent '0', 49 to represent '1', ..., 48 + 9 to represent '9'. By subtracting 48, you convert the ASCII character into its integer value.

    It's more readable to use (x - '0') than (x - 48), though, and it does not assume you're using the ASCII character set.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Rashakil Fol
    It's more readable to use (x - '0') than (x - 48), though, and it does not assume you're using the ASCII character set.
    It's not only more readable, but safer. There is no guarentee that '0' == 48 (although I have never seen anything else).

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    but what about atoi to change the character to intergers?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That doesn't change characters. It converts strings, which are series of characters terminated by a null character.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Quote Originally Posted by Ancient Dragon
    It's not only more readable, but safer. There is no guarentee that '0' == 48 (although I have never seen anything else).
    EBCDIC stands out as the obvious place where '0' != 48.
    http://en.wikipedia.org/wiki/EBCDIC#Codepage_layout

    The OP might be interested in http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  3. covert ascii code to character
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 04-25-2002, 09:53 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM