如何在C#中获取字符串的ASCII值

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

How to get ASCII value of string in C#

c#encodingascii

提问by RBS

I want to get the ASCII value of characters in a string in C#.

我想在 C# 中获取字符串中字符的 ASCII 值。

If my string has the value "9quali52ty3", I want an array with the ASCII values of each of the 11 characters.

如果我的字符串的值为“9quali52ty3”,我想要一个包含 11 个字符中每个字符的 ASCII 值的数组。

How can I get ASCII values in C#?

如何在 C# 中获取 ASCII 值?

采纳答案by Neil Barnwell

From MSDN

来自MSDN

string value = "9quali52ty3";

// Convert the string into a byte[].
byte[] asciiBytes = Encoding.ASCII.GetBytes(value);

You now have an array of the ASCII value of the bytes. I got the following:

您现在有一个字节的 ASCII 值数组。我得到以下信息:

57 113 117 97 108 105 53 50 116 121 51

57 113 117 97 108 105 53 50 116 121 51

回答by LeppyR64

string s = "9quali52ty3";
foreach(char c in s)
{
  Console.WriteLine((int)c);
}

回答by jason

This should work:

这应该有效:

string s = "9quali52ty3";
byte[] ASCIIValues = Encoding.ASCII.GetBytes(s);
foreach(byte b in ASCIIValues) {
    Console.WriteLine(b);
}

回答by Lars Truijens

Do you mean you only want the alphabetic characters and not the digits? So you want "quality" as a result? You can use Char.IsLetter or Char.IsDigit to filter them out one by one.

你的意思是你只想要字母字符而不是数字?所以你想要“质量”作为结果?您可以使用 Char.IsLetter 或 Char.IsDigit 将它们一一过滤掉。

string s = "9quali52ty3";
StringBuilder result = new StringBuilder();
foreach(char c in s)
{
  if (Char.IsLetter(c))  
    result.Add(c);
}
Console.WriteLine(result);  // quality

回答by Rauhotz

string text = "ABCD";
for (int i = 0; i < text.Length; i++)
{
  Console.WriteLine(text[i] + " => " + Char.ConvertToUtf32(text, i));
}

If I remember correctly, the ASCII value is the number of the lower seven bits of the Unicodenumber.

如果我没记错的话,ASCII 值是Unicode数字的低七位的数字。

回答by Neil Barnwell

If you want the charcode for each character in the string, you could do something like this:

如果您想要字符串中每个字符的字符代码,您可以执行以下操作:

char[] chars = "9quali52ty3".ToCharArray();

回答by moleatom

byte[] asciiBytes = Encoding.ASCII.GetBytes("Y");
foreach (byte b in asciiBytes)
{
    MessageBox.Show("" + b);
}

回答by Tony Dallimore

Earlier responders have answered the question but have not provided the information the title led me to expect. I had a method that returned a one character string but I wanted a character which I could convert to hexadecimal. The following code demonstrates what I thought I would find in the hope it is helpful to others.

较早的回复者已经回答了这个问题,但没有提供标题让我期待的信息。我有一个返回一个字符串的方法,但我想要一个可以转换为十六进制的字符。以下代码演示了我认为我会发现的内容,希望对其他人有所帮助。

  string s = "\ta£\x0394\x221A";   // tab; lower case a; pound sign; Greek delta;
                                   // square root  
  Debug.Print(s);
  char c = s[0];
  int i = (int)c;
  string x = i.ToString("X");
  c = s[1];
  i = (int)c;
  x = i.ToString("X");
  Debug.Print(c.ToString() + " " + i.ToString() + " " + x);
  c = s[2];
  i = (int)c;
  x = i.ToString("X");
  Debug.Print(c.ToString() + " " + i.ToString() + " " + x);
  c = s[3];
  i = (int)c;
  x = i.ToString("X");
  Debug.Print(c.ToString() + " " + i.ToString() + " " + x);
  c = s[4];
  i = (int)c;
  x = i.ToString("X");
  Debug.Print(c.ToString() + " " + i.ToString() + " " + x);

The above code outputs the following to the immediate window:

上面的代码将以下内容输出到立即窗口:

a£Δ√

a 97 61

一个 97 61

£ 163 A3

£ 163 A3

Δ 916 394

Δ 916 394

√ 8730 221A

√ 8730 221A

回答by ZagNut

Or in LINQ:

或者在 LINQ 中:

string value = "9quali52ty3";

string value = "9quali52ty3";

var ascii_values = value.Select(x => (int)x);

var ascii_values = value.Select(x => (int)x);

var as_hex = value.Select(x => ((int)x).ToString("X02"));

var as_hex = value.Select(x => ((int)x).ToString("X02"));

回答by mahesh

string value = "mahesh";

// Convert the string into a byte[].
byte[] asciiBytes = Encoding.ASCII.GetBytes(value);

for (int i = 0; i < value.Length; i++)


    {
        Console.WriteLine(value.Substring(i, 1) + " as ASCII value of: " + asciiBytes[i]);
    }