Converting ASCII to Character in C#
To convert ASCII values to characters in C#, we will use the Convert.ToChar
method. This method takes an integer representing an ASCII value as its parameter and returns the corresponding character.
using System;
class Program
{
static void Main(string[] args)
{
int[] asciiValues = { 65, 66, 67, 97, 98, 99 };
Console.WriteLine("ASCII to Character Conversion:");
foreach (int asciiValue in asciiValues)
{
char character = Convert.ToChar(asciiValue);
Console.WriteLine($"ASCII Value: {asciiValue}, Character: {character}");
}
}
}
In the above program, we have defined an array asciiValues
containing few ASCII values. We looped over each value, and converted it to a character using the Convert.ToChar
method.
Output:
ASCII to Character Conversion:
ASCII Value: 65, Character: A
ASCII Value: 66, Character: B
ASCII Value: 67, Character: C
ASCII Value: 97, Character: a
ASCII Value: 98, Character: b
ASCII Value: 99, Character: c
Comments (0)