ASCII is an acronym that stands for American Standard Code for Information Interchange. In ASCII, a specific numerical value is given to different characters and symbols, for computers to store and manipulate, and while storing and manipulating the electronic device always works with the binary value of the ASCII number given. As it is impossible to do that in the original form.
Approaches: There are 4 ways to print ASCII value or code of a specific character which are listed below briefing the concept followed by a java example for the implementation part.
- Using brute force Method
- Using the type-casting Method
- Using the format specifier Method
- Using Byte class Method
Method 1: Assigning a Variable to the int Variable
In order to find the ASCII value of a character, simply assign the character to a new variable of integer type. Java automatically stores the ASCII value of that character inside the new variable.
Implementation: Brute force Method
Java
// Java program to print ASCII Value of Character // by assigning variable to integer public class GFG { // Main driver method public static void main(String[] args) { // Character whose ASCII is to be computed char ch = '}' ; // Creating a new variable of type int // and assigning the character value. int ascii = ch; /* Java stores the ascii value there itself*/ // Printing the ASCII value of above character System.out.println( "The ASCII value of " + ch + " is: " + ascii); } } |
The ASCII value of } is: 125
Method 2: Using Type-Casting
Type-casting in java is a way to cast a variable into another datatype which means holding a value of another datatype occupying lesser bytes. In this approach, a character is a typecast of type char to the type int while printing, and it will print the ASCII value of the character.
Java
// Java program to print ASCII Value of Character // using type-casting // Importing java generic libraries import java.util.*; public class GFG { // Main driver method public static void main(String[] args) { // Character whose ASCII is to be computed char ch = '}' ; // Typecasting the character to int and // printing the same System.out.println( "The ASCII value of " + ch + " is: " + ( int )ch); } } |
The ASCII value of } is: 125
Note: In above method 1 and method 2, both the methods are one type of typecasting. In method 1, typecasting is done automatically by the compiler. In method 2, typecasting it manually so the method 2 is much more efficient than method 1 as the compiler has to put lesser effort. Also, remember typecasting done automatically is called implicit typecasting and where it is done from the user end is called explicit typecasting
Method 3: Using format specifier (More Optimal)
In this approach, we generate the ASCII value of the given character with the help of a format specifier. We have stored the value of the given character inside a formal specifier by specifying the character to be an int. Hence, the ASCII value of that character is stored inside the format specifier.
Java
// Java program to print ASCII Value of Character // using format specifier // Importing format library import java.util.Formatter; public class GFG { // Main driver method public static void main(String[] args) { // Character whose ASCII is to compute char character = '}' ; // Initializing the format specifier Formatter formatSpecifier = new Formatter(); // Converting the character to integer and // ASCII value is stored in the format specifier formatSpecifier.format( "%d" , ( int )character); // Print the corresponding ASCII value System.out.println( "The ASCII value of the character ' " + character + " ' is " + formatSpecifier); } } |
The ASCII value of the character ' } ' is 125
Method 4: Finding the ASCII value by generating byte (Most Optimal)
- Initializing the character as a string.
- Creating an array of type byte by using getBytes() method.
- Printing the element at ‘0’th index of the bytes array.
This is the ASCII value of our character residing at the ‘0’th index of the string. This method is generally used to convert a whole string to their ASCII values. For the characters violating the encoding exception, the try-catch is given.
Java
// Java program to print ASCII Value of Character // by generating bytes. // Importing I/O library import java.io.UnsupportedEncodingException; public class GFG { // Main driver method public static void main(String[] args) { // Try block to check exception try { // Character is initiated as a string String sp = "}" ; // An array of byte type is created // by using getBytes method byte [] bytes = sp.getBytes( "US-ASCII" ); /*This is the ASCII value of the character / present at the '0'th index of above string.*/ // Printing the element at '0'th index // of array(bytes) using charAt() method System.out.println( "The ASCII value of " + sp.charAt( 0 ) + " is " + bytes[ 0 ]); } // Catch block to handle exception catch (UnsupportedEncodingException e) { // Message printed for exception System.out.println( "OOPs!!!UnsupportedEncodingException occurs." ); } } } |
The ASCII value of } is 125