为什么这段代码打印的是 48(0 的 ASCII 码)而不是 0?
char c = '0';
Console.WriteLine(Convert.ToInt32(c));
有什么特殊原因吗?
最佳答案
因为,如 MSDN states ,Convert.ToInt32 方法(Char)返回:
A 32-bit signed integer that represents the UTF-16 encoded code point of the value parameter.
和代码点的意思是:
In character encoding terminology, a code point or code position is any of the numerical values that make up the code space. For example, ASCII comprises 128 code points in the range 0h to 7Fh
从逻辑的角度来看,您希望 Convert.ToInt32('a')
得到什么结果? Convert 是用于类型转换,而不是用于解释。可以通过 Parse-kind 方法进行解释。
关于c# - 为什么 char 的 Convert.ToInt32 返回 ascii 码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20578343/