问题描述
当我将一个整数(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);
推荐答案
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.
将int转换为char在C#?
Converting int to char in C#?
您的代码没有将整数1转换为char'1',它将整数1转换为带有ascii代码1的char。
[]
正如您在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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!