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.

Share:
511,001
RBS
Author by

RBS

I am a senior Application Developer.

Updated on July 09, 2021

Comments

  • RBS
    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
      Nick over 15 years
      Do 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
      RBS over 15 years
      I want Ascii of each character from that string ,Ascii of digits as well as ascii of word "quality"
    • user772401
      user772401 almost 15 years
      What 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
    Nick over 14 years
    The resulting codes are Unicode numbers and could potentially contain non ASCII codes.
  • mahesh
    mahesh over 11 years
    this is also one of example to implement ascii value for each charcter in given string
  • O. Jones
    O. Jones almost 7 years
    This 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
    MA Nadeem almost 7 years
    my 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
    Nikos over 5 years
    This 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
    Tom Blodget over 5 years
    Note: 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 the Encoding 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
    Dharman about 4 years
    While 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
    BDL about 4 years
    Gives a compile error: "Unexpected character". The two ` after the ToString probably shouldn't be there.
  • Leandro Bardelli
    Leandro Bardelli over 2 years
    using System.Text;