How to get ASCII value of string in C#
511,001
Solution 1
From MSDN
string value = "9quali52ty3";
// Convert the string into a byte[].
byte[] asciiBytes = Encoding.ASCII.GetBytes(value);
You now have an array of the ASCII value of the bytes. I got the following:
57 113 117 97 108 105 53 50 116 121 51
Solution 2
string s = "9quali52ty3";
foreach(char c in s)
{
Console.WriteLine((int)c);
}
Solution 3
This should work:
string s = "9quali52ty3";
byte[] ASCIIValues = Encoding.ASCII.GetBytes(s);
foreach(byte b in ASCIIValues) {
Console.WriteLine(b);
}
Solution 4
Do you mean you only want the alphabetic characters and not the digits? So you want "quality" as a result? You can use Char.IsLetter or Char.IsDigit to filter them out one by one.
string s = "9quali52ty3";
StringBuilder result = new StringBuilder();
foreach(char c in s)
{
if (Char.IsLetter(c))
result.Add(c);
}
Console.WriteLine(result); // quality
Solution 5
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.
Comments
-
RBS almost 3 years
I want to get the ASCII value of characters in a string in C#.
If my string has the value "9quali52ty3", I want an array with the ASCII values of each of the 11 characters.
How can I get ASCII values in C#?
-
Nick over 15 yearsDo you mean you only want the alphabetic characters and not the digits? So you want "quality" as a result? Cause then talking about ASCII makes little sense.
-
RBS over 15 yearsI want Ascii of each character from that string ,Ascii of digits as well as ascii of word "quality"
-
user772401 almost 15 yearsWhat you mean is that you want the numeric ASCII value of each character in the string, assuming the entire string can be represented in ASCII. Your current wording is very confusing.
-
-
Nick over 14 yearsThe resulting codes are Unicode numbers and could potentially contain non ASCII codes.
-
mahesh over 11 yearsthis is also one of example to implement ascii value for each charcter in given string
-
O. Jones almost 7 yearsThis answer could use a little more explanation. If you have a colleague more fluent in English, with respect you should ask for help.
-
MA Nadeem almost 7 yearsmy explanation is very simple because my code is very simple without any variables but used a simple concept how can print ASCII value individual simple get a char and convert the char in integer value so please you implement this and if i have mistake in my explanation so please you correct this thanks
-
Nikos over 5 yearsThis shows me System.Byte[]. You need to loop through the chars in the word. Not sure how you got it to work. Helped the OP though which is what matters.
-
Tom Blodget over 5 yearsNote:
Encoding.ASCII
is configured to emit the ASCII code unit for the replacement character ('?') for characters that are not in the ASCII character set. Other options—including throwing an exception—are available in theEncoding
class. And, of course, other character encodings—especially UTF-8—are available, too; One should question whether it is ASCII that is actually wanted. -
Dharman about 4 yearsWhile this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
-
BDL about 4 yearsGives a compile error: "Unexpected character". The two ` after the ToString probably shouldn't be there.
-
Leandro Bardelli over 2 yearsusing System.Text;