Some fonts i.e. Symbola and Segoe ui Symbols have characters with five digit hex assignments. I.e the character "mushroom" is
They show up fine in the character picker in excel etc but this code.
gr.setFont(new Font("Segoe UI Symbol",Font.PLAIN,12); gr.drawString(""+(char)Ox1F344+(char)Ox270B,100,100);
Gives me a unknown character and a hand rather than a mushroom and hand.
What am I doing wrong?
(char)Ox1F344 will overflow, as
char is a 16-bit type and
0x1F344 requires more than 16 bits.
In Java, characters are UTF-16 code units, not actual Unicode characters. So you need the UTF-16 version of the character, which is
0xD83C 0xDF44. Then you need to put that in the string:
gr.drawString("\uD83C\uDF44\u270B", 100, 100); // This -------------------^^^^^^ is unrelated, it's the // second character shown in your question.
String types are, unfortunately, based around UTF-16 code units, so 0x1F344 overflows the 16-bit
char datatype and is the same as 0xF344 (an unstandardised code point in the Private Use Area that could appear as anything).
In UTF-16 to encode a characters outside the Basic Multilingual Plane (0–0xFFFF) you have to use two Surrogate (0xD800–0xDFFF) characters. So the
\u-escaped form of