Program to Find ASCII Value of a Character - (C# Basic Programs)
In C#, ASCII values of characters can be found using the built-in char data type. In this tutorial, we will discuss a program to find the ASCII value of a character in C#.
Syntax
The syntax for finding the ASCII value of a character in C# is as follows:
char ch = 'a'
int ascii = (int) ch;
Example
using System;
class Program {
static void Main(string[] args) {
char ch = 'a';
int ascii = (int) ch;
Console.WriteLine("The ASCII value of " + ch + " is " + ascii);
}
}
Output:
The ASCII value of a is 97
Explanation
In the above example, we first define a character ch
with the value 'a'. We then typecast this character into an integer type using the (int)
casting operator. This gives us the ASCII value of the character 'a', which is 97. We then print out the value of the ASCII code using the Console.WriteLine
method.
Use
Finding the ASCII value of a character in C# is a common operation that is useful in many applications. It is often used when dealing with text processing tasks, such as string comparison, encryption, and data transmission.
Summary
In this tutorial, we have discussed a program to find the ASCII value of a character in C#. We have seen the syntax, example, explanation, use, and importance of finding the ASCII value of a character in C#. By understanding how to find the ASCII value of a character, programmers can perform various text processing operations and manipulate data more effectively.