The ASCII character 255 is actually a non-breaking space, or NBSP!
The ASCII characters are numbered from 0 to 255. The chart below shows their designation in hexadecimal numbers. The ASCII characters are numbered from 00 to FF in hexadecimal. Note that 0 to 9 are the same in both numbering systems.
Below are the implementation of both methods: Using ASCII values: ASCII value of uppercase alphabets – 65 to 90. ASCII value of lowercase alphabets – 97 to 122.
Character.toString ((char) i);
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.
int number = 65;
char c = (char)number;
it is a simple solution
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With