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

135,465

Solution 1

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());

Solution 2

Just cast each character to an int:

for (int i = 0; i < str.length; i++)  
  Console.Write(((int)str[i]).ToString());

Solution 3

This example might help you. by using simple casting you can get code behind urdu character.

string str = "عثمان";
        char ch = ' ';
        int number = 0;
        for (int i = 0; i < str.Length; i++)
        {
            ch = str[i];
            number = (int)ch;
            Console.WriteLine(number);
        }
Share:
135,465
Shamim Hafiz - MSFT
Author by

Shamim Hafiz - MSFT

Help and get helped

Updated on July 31, 2020

Comments

  • Shamim Hafiz - MSFT
    Shamim Hafiz - MSFT almost 4 years

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