java utf 8 转unicode,如何在Java中将UTF-8转换为unicode?

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 is U+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.

解决方案

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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值