Java Program to Print the ASCII Value
Last Updated :
24 May, 2024
ASCII is an acronym that stands for American Standard Code for Information Interchange. In this, a specific numerical value is assigned to different characters and symbols, it is a 7-bit character set containing 128 (0-127), 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. This article focuses on discussing ways to print the ASCII Value.
1. Brute-force Method
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.
Below is the Java program to implement the approach:
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);
}
}
OutputThe ASCII value of } is: 125
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.
Below is the Java program to implement the approach:
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);
}
}
OutputThe 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
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.
Below is the Java program to implement the approach:
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);
}
}
OutputThe ASCII value of the character ' } ' is 125
4. By Generating Byte
Steps to Print the ASCII Value are mentioned below:
- 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.
Below is the Java program to implement the approach:
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.");
}
}
}
OutputThe ASCII value of } is 125