C#で文字列内の文字のASCII値を取得したい。
文字列の値が「9quali52ty3」の場合、11文字のそれぞれのASCII値を含む配列が必要です。
C#でASCII値を取得するにはどうすればよいですか?
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
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 text = "ABCD";
for (int i = 0; i < text.Length; i++)
{
Console.WriteLine(text[i] + " => " + Char.ConvertToUtf32(text, i));
}
私の記憶が正しければ、ASCII 値はUnicode番号の下位 7 ビットの番号です。
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 値を出力します。
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);
}
}
}
またはLINQで:
string value = "9quali52ty3";
var ascii_values = value.Select(x => (int)x);
var as_hex = value.Select(x => ((int)x).ToString("X02"));
byte[] asciiBytes = Encoding.ASCII.GetBytes("Y");
foreach (byte b in asciiBytes)
{
MessageBox.Show("" + b);
}
文字列内の各文字の文字コードが必要な場合は、次のようにすることができます。
char[] chars = "9quali52ty3".ToCharArray();
以前の回答者は質問に答えましたが、タイトルが私に期待させた情報を提供しませんでした. 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
以下を使用してBOMを削除できます。
//Create a character to compare BOM
char byteOrderMark = (char)65279;
if (sourceString.ToCharArray()[0].Equals(byteOrderMark))
{
targetString = sourceString.Remove(0, 1);
}
C# で文字列内の文字の ASCII 値を取得したいと考えています。
誰もがこの構造で答えを与えます。文字列の値が「9quali52ty3」の場合、11 文字それぞれの ASCII 値を含む配列が必要です。
しかし、コンソールでは率直に作業するので、間違っている場合は文字を取得してASCIIコードを出力するので、答えを修正してください。
static void Main(string[] args)
{
Console.WriteLine(Console.Read());
Convert.ToInt16(Console.Read());
Console.ReadKey();
}
string nomFile = "9quali52ty3";
byte[] nomBytes = Encoding.ASCII.GetBytes(nomFile);
string name = "";
foreach (byte he in nomBytes)
{
name += he.ToString("X02");
}
`
Console.WriteLine(name);
// 今は良くなりました ;)