Search code examples
c#countalphabet

Converting letter position in the alphabet to numbers in C#


I need to convert letters into numbers. The A would be 01, the B would be 02 ... Z would be 26.

My pseudocode:

variable = 'C'

if (variable = 'A'){
counter = 01}else if (variable = 'B'){
counter = 02}else if (variable = 'C'){
counter = 03
}elseif...

There sure must be another way to do this.


Solution

  • Try:

    var x = 'A' - 'A' + 1 //01
    var x = 'Z' - 'A' + 1 //26