c# - 为什么 char 的 Convert.ToInt32 返回 ascii 码?

标签 c# char int

为什么这段代码打印的是 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/

相关文章:

c - 如何调试这段c代码

java - ArrayIndexOutOfBoundsException、字符串、Int

objective-c - 将一个字符串后跟一个 int 添加到数组中

c++ - 将一些数据写入 Char

c++ - 将内容复制到 char*

mysql - 更改数据库结构后更新旧的存储过程

c# - 运算符 '==' 不能应用于类型 'System.Windows.Forms.Keys' 和 'int' 的操作数

c# - Serilog ThreadId 始终输出为 1

C#:输入流不是有效的二进制格式

c# - 在现有属性之后添加属性