当前位置:编程学习 > JAVA >>

System.out.println("a".getBytes("unicode").length)为什么打印为4?

 System.out.println("1a".getBytes("unicode").length);打印为6,unicode不是16位表示 一个字符吗? --------------------编程问答-------------------- 大虾来说明下啥,百度不好使 --------------------编程问答-------------------- 16是指01代码吧,你打印"1a".getBytes("unicode")看是什么不就知道了
--------------------编程问答-------------------- 你代码的编码是UTF-8的吧? --------------------编程问答-------------------- 已经给你思路了,上网去查查吧 --------------------编程问答-------------------- 没搞明白,utf-8 和 unicode的 字母和数字编码都是一样的吗,为什么多了-2,-1,0这样的东西,就算凑足16位 ,那字符串前面的-2,-1是什么意思呢? --------------------编程问答-------------------- 同问。应该是4。 --------------------编程问答-------------------- 这个问题不重要,反正是用于转码,管它多大呢。 --------------------编程问答-------------------- UNICODE只是个CHARSET, 有不同的ENCODING去把字符转成UNICOD CODEPOINT. UTF-8, UTF-16 就是其中的ENCODING 方法 --------------------编程问答-------------------- java string 都是用UTF-16来做encoding的,所以如果是一个字母就会返回4bytes, 2 bytes per character.

A String represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character Representations in the Character class for more information). Index values refer to char code units, so a supplementary character uses two positions in a String.

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html --------------------编程问答--------------------
引用 9 楼 soton_dolphin 的回复:
java string 都是用UTF-16来做encoding的,所以如果是一个字母就会返回4bytes, 2 bytes per character.

A String represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character Representations in the Character class for more information). Index values refer to char code units, so a supplementary character uses two positions in a String.

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html


UTF-16 不是2字节,为什么有 4bytes,而且每个字符串前面都会多出2bytes,值为-2,-1 --------------------编程问答-------------------- -2,-1 是应该是unicode的标识符号?因为unicode不是一种实现字符
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,