letters to number

Jul 7, 2011 at 6:02pm
I am trying to convert letters to number values.

For example 'A' and 'a' = 1, 'B' and 'b' = 2 and so on. The only way I have been able to think of how to do this is with a switch statement.

Is there a way I could do this without directly assigning them to the number 1?
Jul 7, 2011 at 6:22pm
underneath, chars are ints, they all have a numeric value. If you take the letter input, convert it to upper or lower case using tolower() or toupper() then subtract the desired amount to leave a number so that they character map onto the numbers you want.

http://www.asciitable.com/
Last edited on Jul 7, 2011 at 6:23pm
Jul 7, 2011 at 7:33pm
Although using ASCII codes is generally valid, it is not universal. The C99 standard requires that the digit characters ('0'-'9') are consecutive but not the letter characters: "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.". EBCDIC is perfectly valid, even though its alphabet is disjointed, meaning this code will not work universally.

If you aren't worried about developing for IBM mainframes using EBCDIC quirkyusername's approach is fine. If you are, use a switch statement.
Jul 7, 2011 at 8:52pm
thanks for the pointers
Topic archived. No new replies allowed.