在C#中将int转换为char? [英] Converting int to char in C#?

查看:598
本文介绍了在C#中将int转换为char?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将一个整数(int)转换为char时,我遇到了一个问题。当我将字符打印到ide时,ide不显示任何内容。请帮助我们。

我有发布以下代码。



我尝试过:



While I was converting a integer(int) to char,I got a problem.When I print the character to the ide, the ide shows nothing.Please help me out guys.
I have posted the code below.

What I have tried:

int a = 1;<br />
char c = (char)a;<br />
Console.WriteLine(c);

推荐答案

检查整数值为1的ASCII码是什么意思,例如这里: Ascii表 - ASCII字符代码和html,八进制,十六进制和十进制图表转换 [ ^ ]



你的1低于整数32(空格)意味着它是不可打印的。



尝试例如with ...
Check what the ASCII code of an integer value of 1 means e.g. here:Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion[^]

Your 1 is below integer 32 (Space) which means it is unprintable.

Try e.g. with ...
int a= 65

......看看有什么区别。它应该给你看一个A。



顺便说一句,也许是你的困惑?

你将整数1转换为char并期望它将显示为1。但不是,正如你在上面提到的表中看到的那样,1的ASCII代码是整数49.



所以再试一次......

... and see the difference.It should Show you an "A".

Btw, maybe your confusion?
You "cast" integer 1 to char and expect it will be shown as "1". But no, as you can see in the table mentioned above, the ASCII code for "1" is integer 49.

So try again with...

int a= 49

...并查看差异。它应该显示为1。



最后,如果你需要给定整数的字符串表示,你可以这样做:

... and see the difference.It should Show you an "1".

Finally in case you need the string representation for a given integer you can do this:

int a= 1;
string myString = a.ToString();







我希望这会有所帮助。




I hope this helps.


Quote:

将int转换为char在C#?

Converting int to char in C#?



您的代码没有将整数1转换为char'1',它将整数1转换为带有ascii代码1的char。

Ascii表 - ASCII字符代码和html,八进制,十六进制和十进制图表转换 [ ^ ]

正如您在ascii表中看到的,char'1'是ascii代码49。


Your code is not converting the integer 1 to char '1', it casting integer 1 to char with ascii code 1.
Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion[^]
As you can see in ascii table char '1' is ascii code 49.


这篇关于在C#中将int转换为char?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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