The term “ASCII” is an acronym of the American Standard Code for Information Interchange. It is composed of 7-bit code ranging from 0 to 127. The ASCII code gives a unique numerical value to every character. It helps the computer to understand and translate the actual meaning of the character based on this numerical value. It is widely practiced in the encoding and decoding mechanism.
This article is a practical walkthrough to determine the character’s ASCII Value in Java.
How to Get the ASCII Value of Char in Java?
The ASCII value is translated into binary format for the machine to understand and manipulate it accordingly. ASCII values are unique for each control character e.g., carriage control and line feed characters, or printable characters e.g., letters, digits, etc.
Character | Decimal ASCII Value | Hex ASCII Value |
---|---|---|
( | 40 | 28 |
a | 97 | 61 |
A | 65 | 41 |
$ | 36 | 24 |
Now, let’s learn different methods to get the ASCII Value of Character in Java.
Method 1: Get ASCII Value of Character Using Type Casting
Type casting is a process of transforming one data type’s value into another using the type casting operator. Type casting can be either performed by the programmer manually or automatically done by the compiler. In the code below, we can determine the character ASCII’s value using type casting.
Inside the main() method, two character variables are declared and initialized with the following values. In the print statement, the numeric value of the given character is calculated using the typecasting (int). Inside the print statement, the inverted commas are special characters in Java that are printed by using the “\””. Note that the ASCII value of the two characters is different due to the case difference:
package charASCII;
public class charASCII {
public static void main(String[] args) {
char asciiValue_1 = 'z'; //lowercase character variable
char asciiValue_2 = 'Z'; //uppercase character variable
System.out.println("ASCII value of " + "\"" + asciiValue_1 + "\"" + " = " + (int) asciiValue_1);
//provides different ASCII value
System.out.println("ASCII value of " + "\"" + asciiValue_2 + "\"" + " = " + (int) asciiValue_2);
}
}
Output
The output is shown below:
Method 2: Get the ASCII Value of the Character Using Brute Force
Another simple approach to get the ASCII value of a character in Java is by assigning the character variable to an integer variable (brute force). Java will automatically determine the ASCII value of the character variable which will be stored in the integer variable.
Within this code, the character variable “character” stores the following respective values. The integer variable “intASCII” stores the “character” variable’s ASCII value. The following print statement displays the output in the inverted commas by using the “\”” as shown in the code below:
package charASCII;
public class charASCII {
public static void main(String[] args) {
char character = 'z';
int intASCII= character;
System.out.println("ASCII value of " + "\"" + character + "\"" + " = " + intASCII);
}
}
Output
Output is given as follows:
Bonus Tip: Get the Character Using the ASCII Value
You can also determine the character by using the ASCII value. The following code determines the ASCII value of an integer variable “ascii” with the value “90”. Inside the print statement, specifying “char” is the type casting mechanism that determines the ASCII value of the given “ascii” variable:
Method 3: Get the ASCII Value of Character in a String
The charAt() method combined with the type casting or brute force method can also be used to get the ASCII value of a character. In this section, we will be using type casting with the charAt() method to determine the ASCII value of a specific character in a string.
For this, declare and initialize a variable of string data type “str1” inside the main() method. Within the print statement, the “str1” variable invokes the charAt() method that displays the character at the 6th index in the string. The ASCII value of this character is calculated using type casting and the result is printed as shown in the code below:
package charASCII;
public class charASCII {
public static void main(String[] args) {
String str1= "Welcome to JavaBeat";
System.out.println("ASCII value is " + "\"" + str1.charAt(6) + "\" " + (int) str1.charAt(6));
}
}
Output
The output is given as follows:
Method 4: Get the ASCII Character Using the Bytes Array
The Byte array in Java stores the data in the form of binary which ranges from 0 to 128 bits of memory. This feature enables the Byte array to store the data from 0 to 255 values. In this section of the article, the Java character’s ASCII value will be determined using the Byte array.
Provide the following import statement to include the “UnsupportedEncodingException” class of the “java.io” package:
import java.io.UnsupportedEncodingException;
Within the try block, the string variable “sp” stores a value “J”. The byte array stores the ASCII value of the “sp” variable i.e., “J” at the 0th index by using the “US-ASCII” character encoding. The print statement then prints the ASCII value of characters stored in the Byte array in the form of bytes:
try {
String sp = "J";
byte[] bytes = sp.getBytes("US-ASCII");
System.out.println("The ASCII value of "+ sp.charAt(0) + " is "+ bytes[0]);
}
The catch block displays any exception that hinders the normal flow of the program as shown in the code below:
catch (UnsupportedEncodingException e) {
System.out.println(e);
}
Complete Code & Output
Below is the output of the code:
Method 5: Get the ASCII Value Using the Format Specifier
The Formatter class from java.util package provides multiple methods for formatting a string or a character. It accepts two arguments i.e., string formatter and the string on which the string formatter will be applied to. You can determine the ASCII value of a character by using this class as shown in the code given below.
The import statement includes the Formatter class from java.util package. Inside the main method, a character variable “charValue” is initialized with the given value. The “formatSpecifier” object of the Formatter class is then created. The formatSpecifier object calls the format() function inside the println() method. The format() method accepts the string format and the character variable which is followed by the data type “int”. This “int” data type determines the ASCII value of the character variable and the string format “%d” is used for integer values:
package charASCII;
import java.util.Formatter;
public class charASCII {
public static void main(String[] args) {
char charValue= 'a';
Formatter formatSpecifier = new Formatter();
System.out.println("The ASCII value is " + formatSpecifier.format("%d", (int)charValue) );
}
}
Output
The output is given as follows:
That is all from this guide.
Conclusion
The computer machines cannot understand the human language i.e., alphabets or characters. The ASCII value assigns a specific numeric value to each character which is translated into binary code for the machine to understand. The ASCII values range from 0 to 127 and are stored in the form of binary within the memory. To get the ASCII value of a char in Java, you can use different approaches such as the Format class, type casting, or brute force method, etc as shown in this guide. This article covers all the possible solutions for determining the ASCII value of a character in Java.