为什么char的Convert.ToInt32返回ascii代码? [英] Why does Convert.ToInt32 of a char returns the ascii code?

查看:138
本文介绍了为什么char的Convert.ToInt32返回ascii代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么此代码打印48(ASCII代码为0)而不是0?

Why does this code prints the 48 (ASCII code for 0) instead of 0?

char c = '0';
Console.WriteLine(Convert.ToInt32(c));

这有什么特别原因吗?

推荐答案

因为 MSDN状态,Convert.ToInt32方法(字符)返回:

Because, as MSDN states, Convert.ToInt32 Method (Char) returns:


表示32位有符号整数值参数的UTF-16编码代码点

,代码点表示:


在字符编码术语中,代码点或代码位置是构成代码空间的任何数值。例如,ASCII包含128个代码点,范围为0h到7Fh

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')?转换用于类型转换,不用于解释。解释可以通过Parse-kind方法完成。

and from just logic point of view, what result would you expect from Convert.ToInt32('a')? Convert is for type conversion, not for interpretation. Interpretation can be done by Parse-kind methods.

这篇关于为什么char的Convert.ToInt32返回ascii代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆