How to convert char to int?
I'm surprised nobody has mentioned the static method built right into System.Char
...
int val = (int)Char.GetNumericValue('8');
// val == 8
how about (for char c
)
int i = (int)(c - '0');
which does substraction of the char value?
Re the API question (comments), perhaps an extension method?
public static class CharExtensions {
public static int ParseInt32(this char value) {
int i = (int)(value - '0');
if (i < 0 || i > 9) throw new ArgumentOutOfRangeException("value");
return i;
}
}
then use int x = c.ParseInt32();