-2

Convert character to its alphabet integer position?

The above line asks a question as to how you get the index numeric value of a letters position in the alphabet. The accepted answer is:

char c = 'A';
//char c = 'b'; you may use lower case character.
int index = char.ToUpper(c) - 64;//index == 1

Can someone please explain the theory of this to me? Why and how does this work?

Community
  • 1
  • 1
RSM
  • 14,540
  • 34
  • 97
  • 144
  • 3
    The "theory" is the representation of letters in the alphabet with numbers that a processor can deal with. The one you quoted is not a very good theory, about 4 billion people don't do that at all. Give or take a billion. – Hans Passant Apr 14 '15 at 22:30

4 Answers4

4

65 is the decimal value for A. After which the alphabets decimal values increase by 1. You can look at the ASCII table here for reference. http://www.asciitable.com/

Subtracting from 64, will then give you the index of the alphabets relative to an index starting at 1.

Praveen Paulose
  • 5,741
  • 1
  • 15
  • 19
2

Converting a char to int will assign the ASCII numerical value to the int (Because each character is really just stored as a number). ASCII "a" is equal to 97, and uppercase "A" is equal to 65. Converting using ToUpper() means that each value will fall between 65 and 90 (For alphabetical letters at least).

Now that all of the letters are in the same range, subtracting 65 will result in the "index" of the letter starting at 1. See this handy ASCII decimal chart.

Cyral
  • 13,999
  • 6
  • 50
  • 90
1

In ASCII every letter is represented by a number. 'A' is represented by the number 65. Therefore if you want to know the index of a letter it is enough to subtract 64 from it.

Benjy Kessler
  • 7,356
  • 6
  • 41
  • 69
1

Have a look at ASCII code. Characters are just stored as numbers, but the computer knows which number stands for which character. So 65 is A and 66 is B (a has another number than A). You could write this even more readable:

char.ToUpper(c) - 'A'; // in your case + 1, because A has index 1 not 0
maraca
  • 8,468
  • 3
  • 23
  • 45
  • Not sure if `c` and `t` being lowercase makes code so much more readable so... Also both Java and C# don't use ASCII - they use subset of Unicode which "happen" to match ASCII table. – Alexei Levenkov Apr 14 '15 at 22:33
  • I agree it is a matter of taste. But if you don't know the ASCII table by heart it sure is the faster way. – maraca Apr 14 '15 at 22:34
  • I suggest to edit out "Java is more readable" as it has absolutely nothing to do with the question... Capitalization conventions for methods don't make such a big difference and really personal preference: `char.toUpper(c)-'A'` vs. `char.ToUpper(c)-'A'` ... – Alexei Levenkov Apr 14 '15 at 22:38
  • I just put that with Java in there because I need someone to confirm it also works in C# that way... or it doesn't. And if it doesn't it still could be helpful, because the question is more about the concept, C# and Java just happen to be two examples. – maraca Apr 14 '15 at 22:40