Search code examples
c#stringasciicharacter

Getting The ASCII Value of a character in a C# string


Consider the string:

string str="A C# string";

What would be most efficient way to printout the ASCII value of each character in str using C#.


Solution

  • Here's an alternative since you don't like the cast to int:

    foreach(byte b in System.Text.Encoding.UTF8.GetBytes(str.ToCharArray()))
        Console.Write(b.ToString());