How to convert char to int?

tvr picture tvr · Sep 8, 2010 · Viewed 225.7k times · Source

What is the proper way to convert a char to int? This gives 49:

int val = Convert.ToInt32('1');
//int val = Int32.Parse("1"); // Works

I don't want to convert to string and then parse it.

Answer

Joel Mueller picture Joel Mueller · Sep 8, 2010

I'm surprised nobody has mentioned the static method built right into System.Char...

int val = (int)Char.GetNumericValue('8');
// val == 8