如何在Java中将UTF-8转换为unicode? [英] How to convert UTF-8 to unicode in Java?
问题描述
例如,在表情符号字符集中, U + 1F601
是GRINNING FACE WITH SMILING EYES的unicode值, \ xF0 \ x9F \ x98 \ x81
是此字符的UTF-8字节值。
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
用于沉重的黑心,unicode U + 2764
。
\xE2\x9D\xA4
is for heavy black heart, and the unicode is U+2764
.
所以我的问题是,如果我有一个值为的字节数组(0xF0,0x9F,0x98,0x81,0xE2,0x9D,0xA4)
,那么如何我可以将它转换为Unicode值吗?
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?
对于上面的结果,我想要的是一个值为1F601$ c的String数组$ c>和
2764
。
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.
推荐答案
所以我的问题是,如果我有一个带有值的字节数组(0xF0,0x9F,0x98,0x81),然后我如何将其转换为Unicode值?
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?
只需调用指定数据和编码的 String
构造函数:
Simply call the String
constructor specifying the data and the encoding:
String text = new String(bytes, "UTF-8");
您可以指定 Charset
而不是编码名称 - 我喜欢番石榴的简单 Charsets
class,允许你写:
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);
或者对于Java 7,使用 StandardCharsets
,甚至不需要番石榴:
Or for Java 7, use StandardCharsets
without even needing Guava:
String text = new String(bytes, StandardCharsets.UTF_8);
这篇关于如何在Java中将UTF-8转换为unicode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!