C#中将char转换为int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/239103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:21:58  来源:igfitidea点击:

Convert char to int in C#

c#charint

提问by KeithA

I have a char in c#:

我在 C# 中有一个字符:

char foo = '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:

现在我想把 2 变成一个 int。我发现 Convert.ToInt32 返回字符的实际十进制值而不是数字 2。以下将起作用:

int bar = Convert.ToInt32(new string(foo, 1));

int.parse only works on strings as well.

int.parse 也只适用于字符串。

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.

C# 中是否没有本机函数可以在不使其成为字符串的情况下从 char 转换为 int?我知道这是微不足道的,但没有什么可以直接进行转换似乎很奇怪。

采纳答案by Chad Grant

Interesting answers but the docs say differently:

有趣的答案,但文档的说法不同:

Use the GetNumericValuemethods to convert a Charobject that represents a number to a numeric value type. Use Parseand TryParseto convert a character in a string into a Charobject. Use ToStringto convert a Charobject to a Stringobject.

使用这些GetNumericValue方法将Char表示数字的对象转换为数值类型。使用 ParseTryParse将字符串中的字符转换为Char对象。使用ToString一个转换Char对象的String对象。

http://msdn.microsoft.com/en-us/library/system.char.aspx

http://msdn.microsoft.com/en-us/library/system.char.aspx

回答by Jeremy Ruten

This will convert it to an int:

这会将其转换为 int:

char foo = '2';
int bar = foo - '0';

This works because each character is internally represented by a number. The characters '0' to '9' are represented by consecutive numbers, so finding the difference between the characters '0' and '2' results in the number 2.

这是有效的,因为每个字符在内部由一个数字表示。字符 '0' 到 '9' 由连续的数字表示,因此找到字符 '0' 和 '2' 之间的差异会得到数字 2。

回答by sontek

char c = '1';
int i = (int)(c-'0');

and you can create a static method out of it:

您可以从中创建一个静态方法:

static int ToInt(this char c)
{
    return (int)(c - '0');
}

回答by faulty

Has anyone considered using int.Parse()and int.TryParse()like this

有没有人考虑过使用int.Parse()int.TryParse()喜欢这个

int bar = int.Parse(foo.ToString());

Even better like this

像这样更好

int bar;
if (!int.TryParse(foo.ToString(), out bar))
{
    //Do something to correct the problem
}

It's a lot safer and less error prone

它更安全,更不容易出错

回答by RollerCosta

Try This

尝试这个

char x = '9'; // '9' = ASCII 57

int b = x - '0'; //That is '9' - '0' = 57 - 48 = 9

回答by Nikolay

By default you use UNICODE so I suggest using faulty's method

默认情况下您使用 UNICODE 所以我建议使用错误的方法

int bar = int.Parse(foo.ToString());

int bar = int.Parse(foo.ToString());

Even though the numeric values under are the same for digits and basic Latin chars.

即使数字和基本拉丁字符下的数值相同。

回答by Renán Díaz

This worked for me:

这对我有用:

int bar = int.Parse("" + foo);

回答by Dan Friedman

This converts to an integer and handles unicode

这将转换为整数并处理 unicode

CharUnicodeInfo.GetDecimalDigitValue('2')

CharUnicodeInfo.GetDecimalDigitValue('2')

You can read more here.

您可以在此处阅读更多内容。

回答by antonio

I'm using Compact Framework 3.5, and not has a "char.Parse" method. I think is not bad to use the Convert class. (See CLR via C#, Jeffrey Richter)

我使用的是 Compact Framework 3.5,并且没有“char.Parse”方法。我认为使用 Convert 类还不错。(通过 C# 参见 CLR,Jeffrey Richter)

char letterA = Convert.ToChar(65);
Console.WriteLine(letterA);
letterA = 'あ';
ushort valueA = Convert.ToUInt16(letterA);
Console.WriteLine(valueA);
char japaneseA = Convert.ToChar(valueA);
Console.WriteLine(japaneseA);

Works with ASCII char or Unicode char

适用于 ASCII 字符或 Unicode 字符

回答by Slai

Comparison of some of the methods based on the result when the character is not an ASCII digit:

当字符不是ASCII数字时,根据结果比较一些方法:

char c = '\n';                              
Debug.Print($"{c & 15}");                   // 10
Debug.Print($"{c ^ 48}");                   // 58
Debug.Print($"{c - 48}");                   // -38
Debug.Print($"{(uint)c - 48}");             // 4294967258
Debug.Print($"{char.GetNumericValue(c)}");  // -1