字符串的getbytes方法不是我想象的那样

import java.text.MessageFormat; /* * @author majb */ public class StringCharTest { private static final int LENGTH = 4; /** * @param args */ public static void main(String[] args) { try{ String string = "测试"; byte[] bytes = string.getBytes(); System.out.println(bytes.length == LENGTH);//Expect true, but was false. System.out.println(new String(bytes)); int length = bytes.length; int i = 0; while(i < length) { System.out.print(MessageFormat.format("The bytes from {0} to {1} is :", i, i+2)); System.out.println(new String(bytes, i, 2)); i += 2; } bytes = null; bytes = string.getBytes("GB2312"); System.out.println(bytes.length);// The length is 6. System.out.println(new String(bytes, "GB2312")); }catch(Exception e){ // do nothing } } }

大家都知道字符串getbytes方法如果没有charsetName参数的话,是采用Java默认的Unicode编码方式把字符串转化成字节数组,而Unicode编码是采用2个字节来表示每个字符。按照这个推理上面例子中"测试".getBytes().length就是4了,但是结果确实6。通过遍历每个字符发现,好像在前面自动加了""。

而对于GB2312等其他编码方式来说没有上面的问题。

你可能感兴趣的:(exception,String,测试,Class,import,byte)