+ 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); } }
4 Respuestas
+ 1
int x = 1;
char foo = (char) x;
not the cleanest way but should work.
+ 3
Try Integer.toString(1).charAt(0);
+ 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.
0
when I try this way, no one value is showed when I compile...