Mohanapriya R Mohanapriya R
Updated date Jun 20, 2024
In this article, we will learn how to convert characters to ASCII values in C#.
  • 3.8k
  • 0
  • 0

Convert Character to ASCII in C#

Below is a simple C# program that shows how to convert a character to its ASCII value:

using System;

class Program
{
    static void Main()
    {
        char character = 'T';
        int asciiValue = (int)character;
        Console.WriteLine($"The ASCII value of {character} is {asciiValue}");
    }
}

In the above program, we have declared a character variable character and assign it the value 'A'. Then, we converted the character to its ASCII value using the (int) casting operator and stored the result in the asciiValue variable. 

Output:

The ASCII value of T is 84

Here, 'T' has an ASCII value of 84 according to the ASCII table.

But what about non-alphabetic characters or special symbols? C# handles those cases easily as well.

using System;

class Program
{
    static void Main()
    {
        char character = '%';
        int asciiValue = (int)character;
        Console.WriteLine($"The ASCII value of {character} is {asciiValue}");
    }
}

Output:

The ASCII value of % is 37

As you can see, even special symbols like '%' have their own ASCII values.

Comments (0)

There are no comments. Be the first to comment!!!