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

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番号の下位 7 ビットの番号です。

于 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

または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 に答える
3
byte[] asciiBytes = Encoding.ASCII.GetBytes("Y");
foreach (byte b in asciiBytes)
{
    MessageBox.Show("" + b);
}
于 2011-07-21T10:25:00.430 に答える
3

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

char[] chars = "9quali52ty3".ToCharArray();
于 2008-12-30T16:31:04.777 に答える
2

以前の回答者は質問に答えましたが、タイトルが私に期待させた情報を提供しませんでした. 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£Δ√

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 に答える
-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 に答える