+ 1

How to convert an 'int' to 'char'

I'm trying to convert an int to char, as in a code below, but i'm not succeding. How can I do it? Am I forgetting something? public class Program { public static void main(String[] args) { int x = 1; char y = (char)x; System.out.println("Valor de y: " + y); } }

31st Jul 2017, 1:34 AM
Bruno Anastacio
Bruno Anastacio - avatar
4 Réponses
+ 1
int x = 1; char foo = (char) x; not the cleanest way but should work.
31st Jul 2017, 1:36 AM
Jordan Chapman
Jordan Chapman - avatar
+ 3
Try Integer.toString(1).charAt(0);
31st Jul 2017, 5:46 AM
Limitless
Limitless - avatar
+ 1
The program is working, but the output is invisible. When you print a char variable, it displays the ASCII character; not the value. This program sets char y equal to 1, which is nonprintable character CTRL-A. Non-printable characters are 0 through 31, plus 127 (delete). Space (32) might also deceive you in this case. Try setting x to values within the range 33 through 126 and then you will understand how it works.
15th Nov 2017, 12:44 PM
Brian
Brian - avatar
0
when I try this way, no one value is showed when I compile...
31st Jul 2017, 1:53 AM
Bruno Anastacio
Bruno Anastacio - avatar