C# 如何将数字转换为 ASCII 字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17730706/
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-10 10:20:21  来源:igfitidea点击:

How To Convert A Number To an ASCII Character?

c#windowsfunctionasciiwindows-forms-designer

提问by Cyrax Nguyen

I want to create an application where user would input a number and the program will throw back a character to the user.

我想创建一个应用程序,用户可以在其中输入一个数字,然后程序会将一个字符返回给用户。

Edit: How about vice versa, changing a ascii character into number?

编辑:反之亦然,将 ascii 字符更改为数字怎么样?

回答by Farhad Jabiyev

You can use one of these methods to convert number to an ASCII / Unicode / UTF-16character:

您可以使用以下方法之一将数字转换为ASCII / Unicode / UTF-16字符:

You can use these methods convert the value of the specified 32-bit signed integer to its Unicode character:

您可以使用这些方法将指定的 32 位有符号整数的值转换为其 Unicode 字符:

char c = (char)65;
char c = Convert.ToChar(65); 

Also, ASCII.GetStringdecodes a range of bytes from a byte array into a string:

此外,ASCII.GetString将字节数组中的一系列字节解码为字符串:

string s = Encoding.ASCII.GetString(new byte[]{ 65 });

Keep in mind that, ASCIIEncodingdoes not provide error detection. Any byte greater than hexadecimal 0x7F is decoded as the Unicode question mark ("?").

请记住,ASCIIEncoding不提供错误检测。任何大于十六进制 0x7F 的字节都被解码为 Unicode 问号(“?”)。

回答by sora0419

you can simply cast it.

你可以简单地投射它。

char c = (char)100;

回答by Cemafor

Edit: By request, I added a check to make sure the value entered was within the ASCII range of 0 to 127. Whether you want to limit this is up to you. In C# (and I believe .NET in general), chars are represented using UTF-16, so any valid UTF-16 character value could be cast into it. However, it is possible a system does not know what every Unicode character should look like so it may show up incorrectly.

编辑:根据要求,我添加了一个检查以确保输入的值在 0 到 127 的 ASCII 范围内。是否要限制这取决于您。在 C#(我相信 .NET 一般)中,chars 使用 UTF-16 表示,因此可以将任何有效的 UTF-16 字符值转换为它。但是,系统可能不知道每个 Unicode 字符应该是什么样子,因此它可能会显示不正确。

// Read a line of input
string input = Console.ReadLine();

int value;
// Try to parse the input into an Int32
if (Int32.TryParse(input, out value)) {
    // Parse was successful
    if (value >= 0 and value < 128) {
        //value entered was within the valid ASCII range
        //cast value to a char and print it
        char c = (char)value;
        Console.WriteLine(c);
    }
}

回答by StevenTsooo

To get ascii to a number, you would just cast your char value into an integer.

要将 ascii 转换为数字,您只需将 char 值转换为整数即可。

char ascii = 'a'
int value = (int)ascii

Variable value will now have 97 which corresponds to the value of that ascii character

变量值现在将有 97 对应于该 ascii 字符的值

(Use this link for reference) http://www.asciitable.com/index/asciifull.gif

(使用此链接作为参考) http://www.asciitable.com/index/asciifull.gif

回答by Olxi Suresh

C# represents a character in UTF-16 coding rather than ASCII. Therefore converting a integer to character do not make any difference for A-Z and a-z. But I was working with ASCII Codes besides alphabets and number which did not work for me as system uses UTF-16 code. Therefore I browsed UTF-16 code for all UTF-16 character. Here is the module :

C# 用 UTF-16 编码而不是 ASCII 表示字符。因此,将整数转换为字符对 AZ 和 az 没有任何区别。但是我正在使用除字母和数字之外的 ASCII 代码,这对我不起作用,因为系统使用 UTF-16 代码。因此,我浏览了所有 UTF-16 字符的 UTF-16 代码。这是模块:

void utfchars()
{
 int i, a, b, x;
 ConsoleKeyInfo z;
  do
  {
   a = 0; b = 0; Console.Clear();
    for (i = 1; i <= 10000; i++)
    {
     if (b == 20)
     {
      b = 0;
      a = a + 1;
     }
    Console.SetCursorPosition((a * 15) + 1, b + 1);
    Console.Write("{0} == {1}", i, (char)i);
   b = b+1;
   if (i % 100 == 0)
  {
 Console.Write("\n\t\t\tPress any key to continue {0}", b);
 a = 0; b = 0;
 Console.ReadKey(true); Console.Clear();
 }
}
Console.Write("\n\n\n\n\n\n\n\t\t\tPress any key to Repeat and E to exit");
z = Console.ReadKey();
if (z.KeyChar == 'e' || z.KeyChar == 'E') Environment.Exit(0);
} while (1 == 1);
}

回答by Rafe

I was googling about how to convert an int to char, that got me here. But my question was to convert for example int of 6 to char of '6'. For those who came here like me, this is how to do it:

我在谷歌上搜索如何将 int 转换为 char,这让我来到了这里。但我的问题是将例如 6 的 int 转换为 '6' 的字符。对于像我一样来到这里的人,这是如何做到的:

int num = 6;
num.ToString().ToCharArray()[0];