how to convert an ascii number to character in java

how to convert an ascii number to character in java

To convert an ASCII number to a character in Java, you can use the char data type and the (char) casting operator.

Here is an example of how you can convert an ASCII number to a character in Java:

ref‮e‬r to:lautturi.com
int ascii = 65;
char c = (char) ascii;

This code assigns the ASCII number 65 (corresponding to the character 'A') to the variable ascii, and then converts it to the character 'A' using the (char) casting operator. The result is stored in the c variable.

You can also use the Character.toChars method to convert an ASCII number to a character, like this:

int ascii = 65;
char[] chars = Character.toChars(ascii);
char c = chars[0];

This code assigns the ASCII number 65 to the variable ascii, and then calls the Character.toChars method to convert it to an array of characters. The result is stored in the chars array, and the first element of the array (the character 'A') is assigned to the c variable.

You can use these approaches to convert any ASCII number to the corresponding character, as long as the number is within the ASCII range (0 to 127).

Created Time:2017-11-01 12:05:14  Author:lautturi