Java

How to Convert an ASCII Code to char in Java?

ASCII is the abbreviation of “American Standard Code for Information Interchange”. A computer knows the language in numeric form. Therefore, ASCII is used to communicate with computers by exchanging information. All keyboard characters, including all alphabets, numbers, and special characters, comprise a unique ASCII code that the computer understands to process the typed key.

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:

char asciiToChar = (char) ascii;

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”:

int ascii = 69;

Now, we will convert the created variable to a character using Type Casting:

char asciiToChar = (char) ascii;

Lastly, we will print the resultant character “ascii”:

System.out.println("Ascii " +ascii " is a value of Character " + asciiToChar);

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

Character.toString(ascii)

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”:

int ascii = 75;

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:

String asciiToChar = Character.toString(ascii);

Lastly, execute the “System.out.println()” method to print out the required value:

System.out.println("Ascii " +ascii+ " is a value of Character " + asciiToChar);

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

Character.toChars(ascii);

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:

int ascii = 116;

Then, we will call the “Character.toChars()” method, pass “ascii” as an argument, and store the returned char array in “asciiToChar”:

char[] asciiToChar = Character.toChars(ascii);

Lastly, we will print the output on the console:

System.out.print("Ascii " +ascii+ " is a value of Character: ");
System.out.println(asciiToChar);

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.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.