C#中的角色转换得到两个不同的结果 [英] character casting in C# getting two different results
本文介绍了C#中的角色转换得到两个不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我将一个角色投射到int时,我得到了不同的价值。为什么
Console.WriteLine(( int )a);
Console.WriteLine(( int )' a');
结果是
97
347
为什么我得到这两个不同的结果。 ?
解决方案
因为它们是不同的东西?
在第一个声明a
是一个变量,请使用改善问题向我们展示它是什么类型以及你设置它的价值。
在第二个声明你正在看一个文字字符'a'
。我不知道为什么报告347,它应该是97,我怀疑你是在歪曲事实。
结果不是你的代码行进。 char 347是 ''' ,你可能在其他地方更改了变量的值。
使用系统;
public class 测试
{
public static void Main()
{
var a = ' a'< /跨度>;
Console.WriteLine(( int )a);
Console.WriteLine(( int )' 一个跨度>);
}
}
结果:
97
97
when i cast a character to int I am getting different value. why
Console.WriteLine((int)a);
Console.WriteLine((int)'a');
result is
97
347
why I am getting these two different results. ?
解决方案
Because they're different things?
In the first statementa
is a variable, please use Improve question to show us what type it is and what value you set it to.
In the second statement you are looking at a literal character'a'
. I don't know why it is reporting 347, it should be 97, I suspect you are misrepresenting the situation.
results are not marching with your code. char 347 is 'ś', you may have change the value of variable somewhere else.
using System; public class Test { public static void Main() { var a ='a'; Console.WriteLine((int)a); Console.WriteLine((int)'a'); } }
result :
97 97
这篇关于C#中的角色转换得到两个不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文