EN
Java - convert character to ASCII code
0 points
In this article, we would like to show you how to convert a character to ASCII code in Java.
Quick solution:
xxxxxxxxxx
1
char character = 'A';
2
int asciiCode = (int) character;
3
4
System.out.println(asciiCode); // 65
In this example, we cast a character to the int to convert it to the ASCII code.
xxxxxxxxxx
1
public class Example {
2
3
public static void main(String[] args) {
4
char character = 'A';
5
int asciiCode = (int) character;
6
7
System.out.println(asciiCode); // 65
8
}
9
}
The cast is not required explicitly but improves readability.
This solution will also work:
xxxxxxxxxx
1
public class Example {
2
3
public static void main(String[] args) {
4
char character = 'A';
5
int asciiCode = character;
6
7
System.out.println(asciiCode); // 65
8
}
9
}
Output:
xxxxxxxxxx
1
65