Convert ASCII to char in Java

ASCII stands for American Standard Code for Information Exchange. ASCII codes are widely used in Computer Science. Let us see how to convert ASCII values to char in Java.

ASCII values are basically integer values that can be converted to char value just by typecasting. Let us look at 3 ways to do convert ASCII to char through an example Java code:

public class AsciiToChar {

	public static void main(String[] args) {
		
		int num = 97;
		// typecasting ASCII code to char value
		char ch1 = (char) num;
		System.out.println("Typecasting integer ASCII code to char = "+ch1);
		
		
		//we can also directly assign ASCII code to char value
		char ch2 = 98;
		System.out.println("Directly assigning ASCII code to char = "+ch2);
		
		
		// we can also directly print the ASCII value by typecasting it
		System.out.println("Typecasting ASCII code to char directly = "+(char)99);	
	}

}

Output

Typecasting integer ASCII code to char = a
Directly assigning ASCII code to char = b
Typecasting ASCII code to char directly = c

Check this website for amazing java and other technical resources :- https://czt.jouwpagina.nl/

Leave a Comment