This blog will discuss converting an ASCII code to a character in Java.
How to Convert an ASCII Code to char in Java?
For converting an ASCII code to a character in Java, there are different methods listed below:
- Type casting
- toString() method
- toChars() method
Let’s check the functionality of each of these methods with examples.
Method 1: To Convert an ASCII Code to char Using Type Casting
Most programmers utilize Type Casting for converting an ASCII code to char in a Java program as it directly converts one data type to another.
Syntax
The syntax for converting ASCII Code to char in Java using the Type Casting method is given as:
“ascii” is the variable that stores a value of data type “int”. The keyword “char” with the parenthesis like “(char)” indicates that the mentioned int type “ascii” variable is typecasted into a character, and the resultant value will be stored in “asciiToChar”.
Let’s check out an example to understand the conversion of ASCII code to char using Type Casting.
Example
Here, we have an integer type variable “ascii” initialized with “69”:
Now, we will convert the created variable to a character using Type Casting:
Lastly, we will print the resultant character “ascii”:
The output indicates that the ASCII code “69” is converted to “E” char:
Let’s check some other methods to convert the ASCII code to char in Java.
Method 2: To Convert an ASCII Code to char Using toString()
The Java wrapper class named “Character” also offers a “toString()” method, which allows us to convert an ASCII code to the string representing the character’s value.
Syntax
Here, “ascii” is an “int” type data variable containing ASCII code that will be converted to a string referring to the corresponding character.
Example
In this example, we have an ASCII value “75” stored in “ascii”:
We will call the “Character.toString()” method by passing the created character as a parameter and then store the returned value in “asciiToChar” String type variable. Now the question is why it is a String type variable, not a character type? Because the toString() method will always return a string:
Lastly, execute the “System.out.println()” method to print out the required value:
As you can see, the given program successfully converted “75” ASCII code to the “K” character:
We have one more method to perform the same operation. So, move to the next section!
Method 3: To Convert an ASCII Code to char Using toChars()
The “toChars()” method of the Character wrapper class can also be utilized to convert an ASCII code to char in a Java program. It returns the output as an array of characters.
Syntax
Here, “ascii” is an integer type variable having ASCII code that is passed to the “Character.toChars()” method. This method will return a character array.
Example
Firstly, we will create a variable named “ascii” having “116” as ASCII code:
Then, we will call the “Character.toChars()” method, pass “ascii” as an argument, and store the returned char array in “asciiToChar”:
Lastly, we will print the output on the console:
Output
We presented the easiest methods to convert ASCII code to char in Java.
Conclusion
To convert ASCII code to a character, you can use different methods such as Type Casting, toString() method, and toChars() method of the Character class. The toString() method will return a character as a String, while the toChars() method returns the array of characters. Type Casting is the most common and easy method to convert an ASCII code to a character, as it directly typecasts the ASCII code to char. This blog discussed the methods used to convert an ASCII code to char in Java.