JAVA 中汉字在不同编码下的字节不同

*/
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。

转载于:https://www.cnblogs.com/TakeaHeader/p/6011994.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值