tvr tvr - 1 year ago 97
C# Question

How to convert char to int?

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 Source

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

int val = (int)Char.GetNumericValue('8');
// val == 8
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download