C#字符为int [英] C# char to int
问题描述
所以我在C#中的字符:
So I have a char in c#:
char foo = '2';
现在我想要得到的2成一个int。我发现Convert.ToInt32返回字符的实际十进制值,而不是数字2下面的工作:
Now I want to get the 2 into an int. I find that Convert.ToInt32 returns the actual decimal value of the char and not the number 2. The following will work:
int bar = Convert.ToInt32(new string(foo, 1));
int.parse仅适用于字符串为好。
int.parse only works on strings as well.
有没有在C#中没有本地函数从字符去没有使它成为一个字符串为int?我知道这是微不足道的,但它只是似乎很奇怪,有什么本土直接进行转换。
Is there no native function in C# to go from a char to int without making it a string? I know this is trivial but it just seems odd that there's nothing native to directly make the conversion.
推荐答案
有趣的答案,但该文档说是不同的:
Interesting answers but the docs say differently:
使用的GetNumericValue方法
转换一个字符对象重新presents
一些为数值型。使用
分析和向的TryParse转换
字符字符串转换成字符
目的。使用的ToString转换为Char
反对的String对象。
Use the GetNumericValue methods to convert a Char object that represents a number to a numeric value type. Use Parse and TryParse to convert a character in a string into a Char object. Use ToString to convert a Char object to a String object.
<一个href=\"http://msdn.microsoft.com/en-us/library/system.char.aspx\">http://msdn.microsoft.com/en-us/library/system.char.aspx
这篇关于C#字符为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!