문제

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을 사용하여 하나씩 필터링 할 수 있습니다.

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

초기 응답자들은이 질문에 답했지만 제목이 저에게 기대 한 정보를 제공하지 않았습니다. 하나의 캐릭터 문자열을 반환하는 메소드가 있었지만 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 A3

Δ 916 394

√ 8730 221a

string text = "ABCD";
for (int i = 0; i < text.Length; i++)
{
  Console.WriteLine(text[i] + " => " + Char.ConvertToUtf32(text, i));
}

내가 올바르게 기억한다면 ASCII 값은 유니 코드 숫자.

문자열의 각 문자에 대한 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"));

C#의 문자열에서 문자의 ASCII 값을 얻고 싶습니다.

모든 사람은이 구조에서 대답을 부여합니다. 내 문자열에 "9quali52ty3"값이 있으면 11 자 각각의 ASCII 값이있는 배열을 원합니다.

그러나 콘솔에서 우리는 Frankness를 사용하여 숯을 얻고 내가 잘못된 경우 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