C #에서 문자열의 ASCII 값을 얻는 방법
C #의 문자열에서 문자의 ASCII 값을 얻고 싶습니다.
내 문자열의 값이 "9quali52ty3"이면 11 자 각각의 ASCII 값이있는 배열이 필요합니다.
C #에서 ASCII 값을 어떻게 얻을 수 있습니까?
에서 MSDN
string value = "9quali52ty3";
// Convert the string into a byte[].
byte[] asciiBytes = Encoding.ASCII.GetBytes(value);
이제 바이트의 ASCII 값 배열이 있습니다. 나는 다음을 얻었다 :
57113117 97108105 53 50116121 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);
}
}
}
Earlier responders have answered the question but have not provided the information the title led me to expect. I had a method that returned a one character string but I wanted a character which I could convert to hexadecimal. The following code demonstrates what I thought I would find in the hope it is helpful to others.
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);
The above code outputs the following to the immediate window:
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));
}
If I remember correctly, the ASCII value is the number of the lower seven bits of the Unicode number.
If you want the charcode for each character in the string, you could do something like this:
char[] chars = "9quali52ty3".ToCharArray();
You can remove the BOM using:
//Create a character to compare BOM
char byteOrderMark = (char)65279;
if (sourceString.ToCharArray()[0].Equals(byteOrderMark))
{
targetString = sourceString.Remove(0, 1);
}
Or in LINQ:
string value = "9quali52ty3";
var ascii_values = value.Select(x => (int)x);
var as_hex = value.Select(x => ((int)x).ToString("X02"));
I want to get the ASCII value of characters in a string in C#.
Everyone confer answer in this structure. If my string has the value "9quali52ty3", I want an array with the ASCII values of each of the 11 characters.
but in console we work frankness so we get a char and print the ASCII code if i wrong so please correct my answer.
static void Main(string[] args)
{
Console.WriteLine(Console.Read());
Convert.ToInt16(Console.Read());
Console.ReadKey();
}
Why not the old fashioned easy way?
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;
}
참고URL : https://stackoverflow.com/questions/400733/how-to-get-ascii-value-of-string-in-c-sharp
'Programming' 카테고리의 다른 글
Xcode 6 스토리 보드 확대 / 축소를 활성화하는 방법은 무엇입니까? (0) | 2020.08.22 |
---|---|
JQuery를 사용한 노란색 페이드 효과 (0) | 2020.08.22 |
Bash의 문자열에서 줄 바꿈을 제거하는 방법 (0) | 2020.08.22 |
jquery를 사용하여 div 내의 텍스트 만 교체 (0) | 2020.08.22 |
오늘 DateTime이 발생하는지 확인하는 방법은 무엇입니까? (0) | 2020.08.22 |