如何将char转换为int?转换为、如何将、char、int

2023-09-02 11:48:05 作者:陽光依舊那麼溫暖

什么是正确的方式转换成一个字符 INT ? 这给 49

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.

推荐答案

我很惊讶没有人提到的内置到 System.Char ...

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

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