【问题标题】:How to get ASCII value of string in C#如何在 C# 中获取字符串的 ASCII 值
【发布时间】:2010-09-28 21:09:06
【问题描述】:

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

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

如何在 C# 中获取 ASCII 值?

【问题讨论】:

  • 你的意思是你只想要字母而不是数字吗?所以你想要“质量”作为结果?因为那么谈论 ASCII 就没有什么意义了。
  • 我想要该字符串中每个字符的 Ascii,数字的 Ascii 以及单词“quality”的 ascii
  • 你的意思是你想要字符串中每个字符的数字 ASCII 值,假设整个字符串可以用 ASCII 表示。您当前的措辞非常混乱。

标签: c# encoding ascii


【解决方案1】:
string s = "9quali52ty3";
foreach(char c in s)
{
  Console.WriteLine((int)c);
}

【讨论】:

  • 生成的代码是 Unicode 数字,可能包含非 ASCII 代码。
【解决方案2】:

这应该可行:

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

【讨论】:

    【解决方案3】:

    你的意思是你只想要字母而不是数字吗?所以你想要“质量”作为结果?您可以使用 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
    

    【讨论】:

      【解决方案4】:
      string text = "ABCD";
      for (int i = 0; i < text.Length; i++)
      {
        Console.WriteLine(text[i] + " => " + Char.ConvertToUtf32(text, i));
      }
      

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

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          【解决方案6】:

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

          【讨论】:

          • 这显示了 System.Byte[]。您需要遍历单词中的字符。不知道你是如何让它工作的。帮助了 OP,但这是最重要的。
          • 注意:Encoding.ASCII 配置为为不属于 ASCII 字符集中的字符发出替换字符 ('?') 的 ASCII 代码单元。 Encoding 类中提供了其他选项(包括引发异常)。当然,其他字符编码(尤其是 UTF-8)也可用;人们应该质疑它是否是真正需要的 ASCII。
          • 使用 System.Text;
          【解决方案7】:
          byte[] asciiBytes = Encoding.ASCII.GetBytes("Y");
          foreach (byte b in asciiBytes)
          {
              MessageBox.Show("" + b);
          }
          

          【讨论】:

            【解决方案8】:

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

              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

            【讨论】:

              【解决方案9】:

              或者在 LINQ 中:

              string value = "9quali52ty3";
              var ascii_values = value.Select(x => (int)x);
              var as_hex = value.Select(x => ((int)x).ToString("X02"));
              

              【讨论】:

                【解决方案10】:
                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]);
                    }
                

                【讨论】:

                • 这也是为给定字符串中的每个字符实现 ascii 值的示例之一
                【解决方案11】:

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

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

                【讨论】:

                  【解决方案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);
                          }
                      }
                  }
                  

                  【讨论】:

                    【解决方案13】:

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

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

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

                     static void Main(string[] args)
                            {
                                Console.WriteLine(Console.Read());
                                Convert.ToInt16(Console.Read());
                                Console.ReadKey();
                            }
                    

                    【讨论】:

                    • 这个答案可能需要更多解释。如果您有一位英语更流利的同事,您应该寻求帮助。
                    • 我的解释很简单,因为我的代码非常简单,没有任何变量,但使用了一个简单的概念,如何打印 ASCII 值单个简单获取一个字符并将字符转换为整数值,所以请你实现这个并如果我的解释有误,请您纠正,谢谢
                    【解决方案14】:

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

                        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】:
                          string nomFile = "9quali52ty3";  
                      
                           byte[] nomBytes = Encoding.ASCII.GetBytes(nomFile);
                           string name = "";
                           foreach (byte he in nomBytes)
                           {
                               name += he.ToString("X02");
                           }
                      `
                           Console.WriteLine(name);
                      

                      // 现在好多了;)

                      【讨论】:

                      • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请edit您的答案添加解释并说明适用的限制和假设。
                      • 给出编译错误:“意外字符”。 ToString 后面的两个 ` 可能不应该存在。
                      猜你喜欢
                      • 2017-09-08
                      • 2023-04-04
                      • 1970-01-01
                      • 2023-03-22
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多