*/
utf-8中文字节长度:6
utf-16中文字节长度:6
UTF-16BE中文字节长度:4
UTF-16LE中文字节长度:4
UTF-32中文字节长度:8
UTF-32BE中文字节长度:8
UTF-32LE中文字节长度:8
unicode中文字节长度:6
GBK中文字节长度:4
GB2312中文字节长度:4
GB18030中文字节长度:4
ISO8859-1中文字节长度:2
BIG5中文字节长度:3
ASCII中文字节长度:2
-------------------------------------------------*/
package com.jlsoft.Demo;
import java.io.UnsupportedEncodingException;
public class ChinaByteLength {
static String [] charset = {"utf-8",
"utf-16",
"UTF-16BE",
"UTF-16LE",
"UTF-32",
"UTF-32BE",
"UTF-32LE",
"unicode",
"GBK",
"GB2312",
"GB18030",
"ISO8859-1",
"BIG5",
"ASCII"};
public static void ByteLength(String charset,String Test) throws UnsupportedEncodingException {
System.out.println(charset+"中文字节长度:"+Test.getBytes(charset).length);
}
public static void main(String[] args) throws UnsupportedEncodingException {
for (int i = 0; i < charset.length; i++) {
ByteLength(charset[i], "中国");
}
}
}
工作中碰到了,所以做个代码总结下,最典型的UTF-8一个汉字字节码长度为3,而GBK中汉字字节码长度为2,英文为1。