ASCII to Character in Java
Convert an integer ASCII code to its corresponding character.
BeginnerModule 1: Basic Java ProgramsExample 17 of 20
ascii-to-character.java
1import java.util.Scanner;23public class Main {4 public static void main(String[] args) {5 Scanner sc = new Scanner(System.in);67 System.out.print("Enter ASCII value (0-127): ");8 int code = sc.nextInt();910 char ch = (char) code;11 System.out.println("Character for ASCII " + code + " = " + ch);1213 sc.close();14 }15}
Output
Enter ASCII value (0-127): 65 Character for ASCII 65 = A
What's going on
We cast the integer value to char to obtain the corresponding character.