How to convert UTF-8 to unicode in Java? -


for example, in emoji char set, u+1f601 unicode value "grinning face smiling eyes", , \xf0\x9f\x98\x81 utf-8 bytes value character.

\xe2\x9d\xa4 heavy black heart, , unicode u+2764.

so question is, if have byte array value (0xf0, 0x9f, 0x98, 0x81, 0xe2, 0x9d, 0xa4), how can convert unicode value?

for above result, want string array value "1f601" , "2764".

i know can write complex method work, hope there library work.

so question is, if have byte array value (0xf0, 0x9f, 0x98, 0x81), how can convert unicode value?

simply call string constructor specifying data , encoding:

string text = new string(bytes, "utf-8"); 

you can specify charset instead of name of encoding - guava's simple charsets class, allows write:

string text = new string(bytes, charsets.utf_8); 

or java 7, use standardcharsets without needing guava:

string text = new string(bytes, standardcharsets.utf_8); 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -