how does one show extended unicode > 0xFFFF?

Some fonts i.e. Symbola and Segoe ui Symbols have characters with five digit hex assignments. I.e the character "mushroom" is Ox1F344.

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?

Answer1:

(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.

Answer2:

""+(char)Ox1F344+(char)Ox270B

Java's char/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

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:how does one show extended unicode > 0xFFFF?