tags:

views:

8872

answers:

6

I want to get Ascii Value of string in C#

My string will have value like "9quali52ty3" this ,so if I just convert it to integer I don't get ascii values of numbers that are in the string

Can any one please help me with getting ascii values in C#

+4  A: 
string s = "9quali52ty3";
foreach(char c in s)
{
  Console.WriteLine((int)c);
}
Jason Lepack
The resulting codes are Unicode numbers and could potentially contain non ASCII codes.
Lars Truijens
+6  A: 

This should work:

string s = "9quali52ty3";
byte[] ASCIIValues = Encoding.ASCII.GetBytes(s);
foreach(byte b in ASCIIValues) {
    Console.WriteLine(b);
}
Jason
+2  A: 

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
Lars Truijens
A: 
string text = "ABCD";
for (int i = 0; i < text.Length; i++)
{
  Console.WriteLine(text[i] + " => " + Char.ConvertToUtf32(text, i));
}

IIRC the ASCII value is the number of the lower 7 bits of the unicode number.

Rauhotz
A: 

If you want the charcode for each character in the string, you could do something like this:

char[] chars = "9quali52ty3".ToCharArray();

Need a bit more information in the post to be more helpful.

Neil Barnwell
+15  A: 

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