enigma
enigma

Reputation: 45

UTF-16 Charecter Printing in JAVA

I have to print UTF-16 character in text box of javafx but this code only prints reference.

new WriteThreadServer(table, "\tU+1F601".getBytes().toString(),main);

second parameter of the function is saved ina string then printed using textbox.settext().

Upvotes: 0

Views: 2236

Answers (2)

Joop Eggen
Joop Eggen

Reputation: 109547

U+1F601 denotes a Unicode code point. It does not fit a single UTF-16 char (16 bits). It also will not automatically be converted; the preceding \t is just a tab char.

This kind of Unicode is written a bit cumbersome:

String s = new String(new int[] { 0x1F601 }, 0, 1);

Or with the preceding tab character '\t':

String s = new String(new int[] { '\t', 0x1F601 }, 0, 2);

String s = "\t" + new String(Character.toChars(0x1F601));

In java String holds Unicode, as array of char, UTF-16. You can then do a setText(s). Mind that the used font must be able to display that code point. A full Unicode font might do, like MS Arial Unicode. These fonts easily measure 35 MB.

Upvotes: 0

Andy Turner
Andy Turner

Reputation: 140328

If you are trying to print a tab, then a ''GRINNING FACE WITH SMILING EYES', you'd need to use:

new WriteThreadServer(table, "\\t\uD83D\uDE01",main);

You are currently:

  • Taking a string literal
  • Converting it to a byte array using your JVM's default charset
  • Calling toString() on the array, resulting in [B@106d69c, or similar.

If you want unicode characters in your string, you need to provide them in the required format. Sites like fileformat.info provide the "C/C++/Java source code" representation.

Upvotes: 2

Related Questions