+ 2

cast int to char cant output directly.

I can output a char, I can convert an integer to a char without any errors, and I can convert an integer to a char then convert it back to output that with no problems. But when I try to cast an integer to char, I can't output that. The code explains much better but can anybody explain this? UPDATE: I get the same problem in c#, shown below. Same with c++. https://code.sololearn.com/cN15zA5D7fO3/?ref=app https://code.sololearn.com/csydPL5rGLrU/?ref=app https://code.sololearn.com/cuF6VOnjzI3r/?ref=ap

31st Jul 2017, 3:40 AM
Jordan Chapman
Jordan Chapman - avatar
4 Respostas
+ 3
The problem is actually simple. I'm just going to stay with Java for now: When you call "(char) 1" the program converts the 1 to an ASCII code. In other words it will use it as a character code. It becomes clearer when you type this instead: "(char) 50" this will give the output "2" The third time you're using (char) 1 then (int) n, you're getting the ASCII character from 1 then getting the integer back again. I hope this cleared a few things up
31st Jul 2017, 5:42 AM
Limitless
Limitless - avatar
+ 2
That makes sense, thank you
31st Jul 2017, 5:43 AM
Jordan Chapman
Jordan Chapman - avatar
+ 2
Oh, you can use Integer.toString(1).charAt(0) to get the Integer as a character. There's probably a faster way around this
31st Jul 2017, 5:49 AM
Limitless
Limitless - avatar
+ 1
Another update: Tried it in visual studio with c# and got same result except showed a question mark symbol. So this is not a bug or anything. Is there something I don't know about?
31st Jul 2017, 4:04 AM
Jordan Chapman
Jordan Chapman - avatar