为什么Convert.ToInt32('1')返回49? [英] Why does Convert.ToInt32('1') returns 49?

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

问题描述


可能重复:

c#将char转换为int

这有效:

int val = Convert.ToInt32("1");

但这不是:

int val = Convert.ToInt32('1'); // returns 49

这不应该转换为实际的int吗?

Shouldn't this convert to actual int?

推荐答案

它将返回字符1的ASCII值

It is returning the ASCII value of character 1

第一条语句将参数视为字符串并将其转换为Int,
第二个将参数视为char并返回其ascii值

The first statement treats the argument as string and converts the value into Int, The second one treats the argument as char and returns its ascii value

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

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