How to convert ASCII code (0-255) to its corresponding character? How to convert ASCII code (0-255) to its corresponding character? java java

How to convert ASCII code (0-255) to its corresponding character?


System.out.println((char)65);would print "A"


String.valueOf(Character.toChars(int))

Assuming the integer is, as you say, between 0 and 255, you'll get an array with a single character back from Character.toChars, which will become a single-character string when passed to String.valueOf.

Using Character.toChars is preferable to methods involving a cast from int to char (i.e. (char) i) for a number of reasons, including that Character.toChars will throw an IllegalArgumentException if you fail to properly validate the integer while the cast will swallow the error (per the narrowing primitive conversions specification), potentially giving an output other than what you intended.