136

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

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

如何在 C# 中获取 ASCII 值?

4

15 回答 15

218

来自MSDN

string value = "9quali52ty3";

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

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

57 113 117 97 108 105 53 50 116 121 51

于 2008-12-30T16:38:49.300 回答
50
string s = "9quali52ty3";
foreach(char c in s)
{
  Console.WriteLine((int)c);
}
于 2008-12-30T16:28:01.943 回答
24

这应该有效:

string s = "9quali52ty3";
byte[] ASCIIValues = Encoding.ASCII.GetBytes(s);
foreach(byte b in ASCIIValues) {
    Console.WriteLine(b);
}
于 2008-12-30T16:29:18.443 回答
9

你的意思是你只想要字母字符而不是数字?所以你想要“质量”作为结果?您可以使用 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
于 2008-12-30T16:29:49.677 回答
5
string text = "ABCD";
for (int i = 0; i < text.Length; i++)
{
  Console.WriteLine(text[i] + " => " + Char.ConvertToUtf32(text, i));
}

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

于 2008-12-30T16:30:58.630 回答
4
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]);
    }
于 2012-12-04T07:20:47.220 回答
4

该程序将接受多个字符并输出它们的 ASCII 值:

using System;
class ASCII
{
    public static void Main(string [] args)
    {
        string s;
        Console.WriteLine(" Enter your sentence: ");
        s = Console.ReadLine();
        foreach (char c in s)
        {
            Console.WriteLine((int)c);
        }
    }
}
于 2014-03-07T10:40:31.593 回答
3

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

char[] chars = "9quali52ty3".ToCharArray();
于 2008-12-30T16:31:04.777 回答
3
byte[] asciiBytes = Encoding.ASCII.GetBytes("Y");
foreach (byte b in asciiBytes)
{
    MessageBox.Show("" + b);
}
于 2011-07-21T10:25:00.430 回答
3

或者在 LINQ 中:

string value = "9quali52ty3";
var ascii_values = value.Select(x => (int)x);
var as_hex = value.Select(x => ((int)x).ToString("X02"));
于 2012-02-27T22:22:35.083 回答
2

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

  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);

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

a£Δ√

一个 97 61

163 英镑 A3

Δ 916 394

√ 8730 221A

于 2011-09-30T14:31:00.133 回答
2

您可以使用以下方法删除BOM

//Create a character to compare BOM
char byteOrderMark = (char)65279;
if (sourceString.ToCharArray()[0].Equals(byteOrderMark))
{
    targetString = sourceString.Remove(0, 1);
}
于 2013-03-09T11:54:36.483 回答
0

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

每个人都在这个结构中给出答案。如果我的字符串的值为“9quali52ty3”,我想要一个包含 11 个字符中每个字符的 ASCII 值的数组。

但是在控制台中,我们坦率地工作,所以如果我错了,我们会得到一个字符并打印 ASCII 码,所以请更正我的答案。

 static void Main(string[] args)
        {
            Console.WriteLine(Console.Read());
            Convert.ToInt16(Console.Read());
            Console.ReadKey();
        }
于 2017-06-24T11:28:33.607 回答
0

为什么不是老式的简单方法?

    public int[] ToASCII(string s)
    {
        char c;
        int[] cByte = new int[s.Length];   / the ASCII string
        for (int i = 0; i < s.Length; i++)
        {
            c = s[i];                        // get a character from the string s
            cByte[i] = Convert.ToInt16(c);   // and convert it to ASCII
        }
        return cByte;
    }
于 2019-01-11T15:15:34.340 回答
-1
    string nomFile = "9quali52ty3";  

     byte[] nomBytes = Encoding.ASCII.GetBytes(nomFile);
     string name = "";
     foreach (byte he in nomBytes)
     {
         name += he.ToString("X02");
     }
`
     Console.WriteLine(name);

// 现在好多了 ;)

于 2020-03-16T15:46:26.123 回答