prgrm prgrm - 3 months ago 13
C# Question

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.

Answer

Try:

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