代码之家  ›  专栏  ›  技术社区  ›  RBS

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

  •  149
  • RBS  · 技术社区  · 17 年前

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

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

    如何在C#中获取ASCII值?

    15 回复  |  直到 12 年前
        1
  •  213
  •   Brig Lamoreaux Brig Lamoreaux    17 年前

    来自 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

        2
  •  48
  •   LeppyR64    17 年前
    string s = "9quali52ty3";
    foreach(char c in s)
    {
      Console.WriteLine((int)c);
    }
    
        3
  •  23
  •   jason    17 年前

    这应该奏效:

    string s = "9quali52ty3";
    byte[] ASCIIValues = Encoding.ASCII.GetBytes(s);
    foreach(byte b in ASCIIValues) {
        Console.WriteLine(b);
    }
    
        4
  •  9
  •   Lars Truijens    17 年前

    你的意思是你只想要字母字符,而不是数字?所以,你想要“质量”作为结果?你可以使用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
    
        5
  •  5
  •   Peter Mortensen Pieter Jan Bonestroo    12 年前
    string text = "ABCD";
    for (int i = 0; i < text.Length; i++)
    {
      Console.WriteLine(text[i] + " => " + Char.ConvertToUtf32(text, i));
    }
    

    如果我没记错的话,ASCII值是 Unicode 号码。

        6
  •  4
  •   VMAtm    13 年前
    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]);
        }
    
        7
  •  3
  •   Peter Mortensen Pieter Jan Bonestroo    12 年前

    此程序将接受多个字符并输出其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);
            }
        }
    }
    
        8
  •  3
  •   Peter Mortensen Pieter Jan Bonestroo    12 年前

    或者在LINQ中:

    string value = "9quali52ty3";
    var ascii_values = value.Select(x => (int)x);
    var as_hex = value.Select(x => ((int)x).ToString("X02"));
    
        9
  •  3
  •   Peter Mortensen Pieter Jan Bonestroo    12 年前

    如果你想得到字符串中每个字符的字符码,你可以这样做:

    char[] chars = "9quali52ty3".ToCharArray();
    
        10
  •  2
  •   Tony Dallimore    14 年前
    byte[] asciiBytes = Encoding.ASCII.GetBytes("Y");
    foreach (byte b in asciiBytes)
    {
        MessageBox.Show("" + b);
    }
    
        11
  •  2
  •   Peter Mortensen Pieter Jan Bonestroo    12 年前

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

      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£Δ√
    

    a 97 61

    163 A3

    916 394

    8730 221A

        12
  •  1
  •   T.S.    6 年前

    您可以删除 BOM 使用:

    //Create a character to compare BOM
    char byteOrderMark = (char)65279;
    if (sourceString.ToCharArray()[0].Equals(byteOrderMark))
    {
        targetString = sourceString.Remove(0, 1);
    }
    
        13
  •  0
  •   MA Nadeem    9 年前

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

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

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

     static void Main(string[] args)
            {
                Console.WriteLine(Console.Read());
                Convert.ToInt16(Console.Read());
                Console.ReadKey();
            }
    
        14
  •  0
  •   F. Harthoorn    7 年前

    为什么不采用老式的简单方法呢?

        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;
        }
    
        15
  •  -1
  •   Monarc Dev    6 年前
        string nomFile = "9quali52ty3";  
    
         byte[] nomBytes = Encoding.ASCII.GetBytes(nomFile);
         string name = "";
         foreach (byte he in nomBytes)
         {
             name += he.ToString("X02");
         }
    `
         Console.WriteLine(name);
    

    //现在好多了;)