EN
C# / .NET - convert character to ASCII code
0 points
In this article, we would like to show you how to convert character to ASCII code in C#.
Quick solution:
xxxxxxxxxx
1
char character = 'A';
2
int ascii = (int)character;
3
4
Console.WriteLine(ascii); // 65
In this example, we cast character to integer to convert it to the ASCII code.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main()
6
{
7
char character = 'A';
8
int ascii = (int)character;
9
10
Console.WriteLine(ascii);
11
}
Output:
xxxxxxxxxx
1
65
The cast is not required explicitly but improves readability.
This solution will also work:
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main()
6
{
7
char character = 'A';
8
int ascii = character;
9
10
Console.WriteLine(ascii); // 65
11
}
12
}