How to convert UTF-8 to unicode in Java?
Solution 1
So my question is, if I have a byte array with value (0xF0, 0x9F, 0x98, 0x81), then how I can convert it into Unicode value?
Simply call the String
constructor specifying the data and the encoding:
String text = new String(bytes, "UTF-8");
You can specify a Charset
instead of the name of the encoding - I like Guava's simple Charsets
class, which allows you to write:
String text = new String(bytes, Charsets.UTF_8);
Or for Java 7, use StandardCharsets
without even needing Guava:
String text = new String(bytes, StandardCharsets.UTF_8);
Solution 2
Simply use String
class:
byte[] bytesArray = new byte[10]; // array of bytes (0xF0, 0x9F, 0x98, 0x81)
String string = new String(bytesArray, Charset.forName("UTF-8")); // covert byteArray
System.out.println(string); // Test result
XWang
Updated on July 21, 2022Comments
-
XWang almost 2 years
For example, in Emoji Char set,
U+1F601
is the unicode value for "GRINNING FACE WITH SMILING EYES", and\xF0\x9F\x98\x81
is the UTF-8 bytes value for this character.\xE2\x9D\xA4
is for heavy black heart, and the unicode isU+2764
.So my question is, if I have a byte array with value
(0xF0, 0x9F, 0x98, 0x81, 0xE2, 0x9D, 0xA4)
, then how I can convert it into Unicode value?For the above result, what I want is a String array with value
"1F601"
and"2764"
.I know I can write a complex method to do this work, but I hope there is already a library to do this work.
-
artbristol over 10 yearsIf you use Java 7's
java.nio.charset.StandardCharsets
you don't even need Guava -
Jon Skeet over 10 years@artbristol: Thanks - I had a quick look, but missed that. Will edit it in.
-
Ponni Radhakrishnan over 9 years@JonSkeet please what's the equivalent in .net or c#
-
Jon Skeet over 9 years@CharlesO: You'd use
Encoding.UTF8.GetBytes(text)
, and in reverse,Encoding.UTF8.GetString(bytes)
. -
Ponni Radhakrishnan over 9 years@JonSkeet I seem to be looking for the wrong thing here... Yes i'm aware we can use the GetBytes and GetString this is what i was unclear about: stackoverflow.com/questions/26151972/…
-
Jon Skeet over 9 years@CharlesO: Okay - I was giving the .NET equivalent of the code in my question, which is what I thought you were after. So do you now have everything you need?
-
Ponni Radhakrishnan over 9 years@JonSkeet Not quite, please see the the question I linked to. above. thanks
-
Jon Skeet over 9 years@CharlesO: Ah, I thought you'd linked to a different question. Will look now.