Reputation: 77
I'm building a custom keyboard with Emojis for Android and, in order to be able to display the Emoji icons, I need to convert values such as "U+1F604" to "\uD83D\uDE04".
This should make it clearer as to what I'm trying to do.
One way would be to transform "1F604" to decimal UTF32, from decimal UTF32 to decimal UTF16 and lastly to hexadecimal UTF16. The next thing would be to convert the resulting 2 numbers into a string like "\uXXXX\uXXXX".
My question is: How can I convert UTF32 into UTF16 in Java? I need some clear pointers (Tried using ICU library, but didn't find a way to do it).
Thanks!
Upvotes: 3
Views: 2072
Reputation: 121710
I don't think this is the real problem. The real problem seems to be "how do I convert a Unicode codepoint into Java characters".
For the example you give, this:
Character.toChars(0x1F604)
will return a two-element char[]
whose elements are 0xD83D and 0xDE04.
Also,
new String(Character.toChars(0x1F604))
will indeed print:
// May or may not display correctly in your browser!
š
which is what you want.
Now, the question remains as to what information you get from your keyboard. If it is a Unicode code point as an int
, such as the method above uses, then you're all set.
Upvotes: 7