美文网首页
Unicode与String

Unicode与String

作者: better_huo | 来源:发表于2019-07-16 18:17 被阅读0次

Unicode介绍

Unicode

PUA

https://unicode-table.com/en/blocks/private-use-area/

String对应的数值数据是由char类型的数组进行保存的,一个char类型占据两个字节,其中char类型是用于存储utf-16格式的字符。

字符串表示采用UTF-16格式的字符串,参考官方说明:

String (Java Platform SE 7 )

字符串表示采用UTF-16格式的字符串,其中补充字符由代理项对表示(有关详细信息,请参阅字符类中的Unicode字符表示部分)。索引值指的是字符代码单位,因此补充字符在字符串中使用两个位置。

字符串类除了提供处理Unicode代码单元(即char值)的方法外,还提供了处理Unicode代码点(即字符)的方法。

String smile ="😀";//等价于String smile ="\uD83D\uDE00";

int codePC = smile.codePointCount(0,smile.length());//1

int code0 = smile.codePointAt(0);

String sCode0 = Integer.toHexString(code0);//1f600

Unicode数值U+1F600转换为utf-16格式后对应的编码值\uD83D\uDE00

3个Unicode单位长度的emoji

unicode.org/emoji

smile ="1️⃣";

String.length方法:返回的是多少个Unicode数目,一个Unicode占据两个字节,即一个char类型。

new String(Character.toChars(0xFB50));length为1,因为占据一个Unicode

new String(Character.toChars(0x1f439));length为2,因为占据两个Unicode

字符串传输过程:

1,A方发送String字符串,通过utf-8字符格式进行编码把utf-16格式的数据转换后变为字节数组发送

2,B方接受到该字节数组,通过public String(byte bytes[], String charsetName),可以通过utf-8格式把字节数组还原成utf-16格式的数据

相关文章

网友评论

      本文标题:Unicode与String

      本文链接:https://www.haomeiwen.com/subject/bcgtlctx.html