Java Program to Print the ASCII Value
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
public class GFG {
public static void main(String[] args)
{
char ch = '}' ;
int ascii = ch;
System.out.println( "The ASCII value of " + ch
+ " is: " + ascii);
}
}
|
Output
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
import java.util.*;
public class GFG {
public static void main(String[] args)
{
char ch = '}' ;
System.out.println( "The ASCII value of " + ch
+ " is: " + ( int )ch);
}
}
|
Output
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
import java.util.Formatter;
public class GFG {
public static void main(String[] args)
{
char character = '}' ;
Formatter formatSpecifier = new Formatter();
formatSpecifier.format( "%d" , ( int )character);
System.out.println(
"The ASCII value of the character ' "
+ character + " ' is " + formatSpecifier);
}
}
|
Output
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
import java.io.UnsupportedEncodingException;
public class GFG {
public static void main(String[] args)
{
try {
String sp = "}" ;
byte [] bytes = sp.getBytes( "US-ASCII" );
System.out.println( "The ASCII value of "
+ sp.charAt( 0 ) + " is "
+ bytes[ 0 ]);
}
catch (UnsupportedEncodingException e) {
System.out.println( "OOPs!!!UnsupportedEncodingException occurs." );
}
}
}
|
Output
The ASCII value of } is 125
Last Updated :
28 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...