• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting an int to a char  RSS feed

 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was reading about the char data type in Java. I know that an unsigned 16 bit integer is used to represent a char. So we can write the assignments below:

But the limit of the char value is 65535.
So I've tried out a few things with char and trying to understand them but I'm not sure how they work.


In the first case I thought that 70000 % 65535 would happen internally and the unicode character present at that location would get stored in 'a' but if I do that I get the answer of 70000 % 65535 as 4465. But when I display 'a' it shows me the output as '?'. Clearly '?' isn't at 4465.
In the second case I have no clue what's happening but somehow when I display 'b' it shows me '?' again.
Can somebody please explain!
Thanks in advance!!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Rao wrote:
So I've tried out a few things with char and trying to understand them but I'm not sure how they work.




When you explicitly cast (ie. *NOT* implicitly cast), the compiler/JVM assumes that you know what you are doing. What happens is the lower 16 bits of the int, is used to form the char. The rest is merely chopped off.

Also, keep in mind that Java uses twos complement for the int format, so, you need to figure out the bits for any negative numbers.

Henry
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Rao wrote:Clearly '?' isn't at 4465.

No, but some other character that cannot be displayed in the font that is being used is at 4465. The '?' is printed instead of the undisplayable character.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Rao wrote: . . . I thought that 70000 % 65535 would happen internally . . .
70000 % 65536, surely. Or more precisely 70000 & 65535 with the bitwise AND operator.

If you look in the Java® Language Specification for narrowing conversions, you find that all that happens for integral types is losing the higher‑order n bits. 70000(decimal) is 0x00011170 or in binary,
0000_0000_0000_0001_0001_0001_0111_0000.
If you take the top 16 bits off that leaves 0001_0001_0111_0000 or 1170(hex) or 4464 decimal. If you look up that Unicode sheet you find 1170 is ᅰ HANGUL JUNGSEONG WE = WE and you can see it in HTML like this: ᅰ = ᅰ. Obviously the command prompt you are using won't display that symbol. The Windows® commend line is very restricted about what symbols it can display and tends to show ? instead.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My bash terminal on Linux wrote:java trivia.HangulDemo (char)65 = A
(char)65535 = ￿
(char)4464 = ᅰ
So it would appear to be a problem with your command line's capabilities. Try this
JOptionPane.showMessageDialog(
    "Chars with casts", String.format("(char)%d = %c", 70000, (char)70000));
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you look up that Unicode sheet you find 1170 is ᅰ HANGUL JUNGSEONG WE = WE and you can see it in HTML like this: ᅰ = ᅰ.


Which apparently is Han Chinese for a ship or similar.

Not that I'll ever remember that, of course.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!