سؤال

وأريد الحصول على قيمة ASCII الأحرف في سلسلة في C #.

إذا خيطي لها قيمة "9quali52ty3" أريد صفيف مع قيم ASCII من كل من الأحرف 11.

وكيف يمكنني الحصول على قيم ASCII في C #؟

نصائح أخرى

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

وهذا يجب أن تعمل:

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

هل يعني أنك تريد فقط الأحرف الأبجدية وليس الأرقام؟ لذلك تريد "نوعية" نتيجة لذلك؟ يمكنك استخدام 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
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]);
    }
byte[] asciiBytes = Encoding.ASCII.GetBytes("Y");
foreach (byte b in asciiBytes)
{
    MessageBox.Show("" + b);
}

وهذا البرنامج لن يقبل أكثر من حرف وإنتاجها قيمة 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);
        }
    }
}
وقد أجاب

والمستجيبين سابقة السؤال ولكن لم تقدم المعلومات عنوان قادني يمكن توقعه. كان لي الطريقة التي عاد سلسلة حرف واحد ولكن كنت أرغب في الطابع الذي أنا يمكن تحويل إلى ست عشرية. توضح التعليمة البرمجية التالية ما ظننت انني سوف تجد على أمل أنه من المفيد للآخرين.

  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

إذا كنت تريد charcode لكل حرف في السلسلة، هل يمكن أن تفعل شيئا من هذا القبيل:

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

ويمكنك إزالة BOM باستخدام:

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

وأو في LINQ:

وstring value = "9quali52ty3";

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

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

وأريد الحصول على قيمة ASCII الأحرف في سلسلة في C #.

والجميع تمنح الجواب في هذا الهيكل. إذا خيطي لها قيمة "9quali52ty3" أريد صفيف مع قيم ASCII من كل من الأحرف 11.

ولكن في وحدة نعمل الصراحة حتى نحصل على شار وطباعة رمز ASCII إذا كنت على خطأ لذا يرجى تصحيح جوابي.

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

لماذا لا القديمة الطراز طريقة سهلة؟

    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;
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top