How to get ASCII value of string in C#


Question

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#?

1
96
7/17/2014 3:51:59 PM

Accepted Answer

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

181
12/30/2008 7:49:53 PM

string s = "9quali52ty3";
foreach(char c in s)
{
  Console.WriteLine((int)c);
}

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon