質問

C#の文字列の文字のASCII値を取得したい。

文字列の値が" 9quali52ty3"の場合、11文字のASCII値を持つ配列が必要です。

C#でASCII値を取得するにはどうすればよいですか

他のヒント

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を使用して、それらを1つずつ除外できます。

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

以前の回答者は質問に回答しましたが、タイトルが私に期待する情報を提供していません。 1文字の文字列を返すメソッドがありましたが、 16進数に変換できる文字が必要でした。次のコードは、他の人に役立つことを期待して私が見つけると思ったことを示しています。

  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; 163 A3

&#916; 916 394

&#8730; 8730 221A

文字列内の各文字の文字コードが必要な場合は、次のようにします。

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 =&quot; 9quali52ty3&quot ;;

var ascii_values = value.Select(x =&gt;(int)x);

var as_hex = value.Select(x =&gt;((int)x).ToString(&quot; X02&quot;));

C#の文字列の文字のASCII値を取得したい。

全員がこの構造で答えを与えます。 文字列の値が「9quali52ty3」の場合、11文字のASCII値を持つ配列が必要です。

ただし、コンソールでは率直に動作するため、文字を取得し、間違っている場合は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