chakri
chakri

Reputation: 629

How to convert UTF16 (emoji) to HTML Entity (hex) using java

How to convert UTF16 (emoji) to HTML Entity (hex) using java


I have the string from DB like this "\uD83D\uDE02". 
I try to display this unicode emoji, it displays as ��. 
I search in google to convert UTF16 to Html hex code. But i didnt get any solution. Please help me

I will show this unicode to Emoji smily icon

Upvotes: 3

Views: 7801

Answers (2)

Chaitanya
Chaitanya

Reputation: 2444

You can use emoji4j library for this.

For example:

String line = "Hi , i am fine \uD83D\uDE02 \uD83D\uDE02, how r u ?";

EmojiUtils.hexHtmlify(line); //Hi , i am fine 😂 😂, how r u ?

Upvotes: 3

Goblin Alchemist
Goblin Alchemist

Reputation: 827

Although the string appears to contain two Unicode characters, it's already one character encoded in UTF-16, that's how Java strings work. You can determine the actual UTF-16-decoded character code using the String.codePointAt method. Here the character's code is 0x1F602, which is Unicode 'FACE WITH TEARS OF JOY': 😂

To write the character to HTML:

OPTION 1: generate the HTML escape entity

String str="\uD83D\uDE02";
FileWriter w=new FileWriter("c:\\temp\\emoji.html");
w.write("<html><body>");
w.write("&#x"+Long.toHexString(str.codePointAt(0))+";");
w.write("</body></html>");
w.close();

This yields

<html><body>&#x1f602;</body></html>

OPTION 2: use some Unicode-capable HTML encoding such as UTF-8

String str="\uD83D\uDE02";
OutputStreamWriter w=new OutputStreamWriter(new FileOutputStream("c:\\temp\\emoji.html"),"UTF-8");
w.write("<html>\n<head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"></head>\n<body>");
w.write(str);
w.write("</body></html>");
w.close();

This yields

<html>
<head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>
<body>рџ‚</body></html>

which is the same happy face encoded in UTF-8.

Upvotes: 2

Related Questions