- public class Test3 {
- public static void main(String[] args) throws IOException {
- byte[] buf = "你好".getBytes("gbk");
- for(byte temp:buf){
- System.out.println(temp);
- }
-
- byte[] buf2 = new byte[]{-60,-29,-70,-61,0,0};
- String str = new String(buf,"utf-8");
- System.out.println(str);
-
- String str1 = new String(buf2, 0, 2, "utf-8");
- String str2 = new String(buf2, 3, 2, "utf-8");
- System.out.println(str1 + str2);
- }
- }
复制代码 "你好"用gbk编码是-60,-29,-70,-61 四个字节,而你用utf-8解码却是以三个字节来解码一个字,所以utf-8解码-60,-29,-70时得出的是??而不是?,上面代码运行可看结果,而剩下的一个-61不足3个字节,会以-61,0,0来自动补0满足3个字节,而utf-8解码-61,0,0是只有?,所以加起来就是???,而不是?或者??。纯手打,望加分 = = |