测试utf-8 汉字和字符等一般占多少个字节

 

package testmain;

public class Testunicode {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  try {
   String str = ",";
   byte[] b = str.getBytes("unicode"); // 从字符串中获得字节
   System.out.println("unicode:"+b.length); //unicode 占 4个字节!!
   b=str.getBytes("utf-8");
   System.out.println("utf-8:"+b.length);  //utf-8汉字英文和中文字符一般占3个字节!!数字和字符一般占1个字节,
   str = new String(b);
   System.out.println(str.getBytes("unicode").length);
  } catch (Exception e) {
   e.printStackTrace();
  }

 }

}

经过上述的测试,得出下列结果:

utf-8汉字英文和中文字符一般占3个字节!!数字和英文字符一般占1个字节,

你可能感兴趣的:(底层技术)