Character to ASCII

Convert a character to its ASCII code.

BeginnerTopic: Module 1: Basic Java Programs
Back

Java Character to ASCII Program

This program helps you to learn the fundamental structure and syntax of Java programming.

Try This Code
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a character: ");
        char ch = sc.next().charAt(0);

        int code = (int) ch;
        System.out.println("ASCII code of " + ch + " = " + code);

        sc.close();
    }
}
Output
Enter a character: a
ASCII code of a = 97

Understanding Character to ASCII

Again, we cast the char to int to see its numeric code.

Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.

Table of Contents