关于Java中各种字符编码格式的说明

 
public class TestString {
	static String zhongwen = "你好";
	static byte[] bytes = null;
	
	public static void main(String[] args) throws Exception {
		// GBK
		main1();
		// utf-8
		//main2();
		// GBK-->utf-8-->GBK
		//main3();
		// GBK-->iso8859-1-->utf-8-->iso8859-1-->GBK
		//main4();

	}
	// GBK
	public static void main1() throws Exception {
		bytes = zhongwen.getBytes("GBK");
		System.out.println(bytes.length);
		for (int i = 0; i < bytes.length; i++) {
			System.out.println(Integer.toHexString(bytes[i]));
		}
		for (int i = 0; i < bytes.length; i++) {
			System.out.println(Integer.toBinaryString(bytes[i]));
		}
		System.out.println(new String(bytes, "iso8859-1"));
	}
	// utf-8
	public static void main2() throws Exception {
		bytes = zhongwen.getBytes("utf-8");
		for (int i = 0; i < bytes.length; i++) {
			System.out.println(Integer.toHexString(bytes[i]));
		}
		System.out.println(new String(bytes, "utf-8"));
	}
	// GBK-->utf-8-->GBK
	public static void main3() throws Exception {
		
		bytes = zhongwen.getBytes("GBK");
		System.out.println("--------GBK----------");
		for (int i = 0; i < bytes.length; i++) {
			System.out.println(Integer.toHexString(bytes[i]));
		}

		bytes = zhongwen.getBytes("utf-8");
		byte [] bytes_copy = bytes;
		System.out.println("-------utf-8--------");
		for (int i = 0; i < bytes.length; i++) {
			System.out.println(Integer.toHexString(bytes[i]));
		}
		
		//错误的组装方法
		System.out.println("-------error GBK--------");
		zhongwen = new String(bytes, "GBK");
		System.out.println(zhongwen);
		bytes = zhongwen.getBytes("GBK");
		for (int i = 0; i < bytes.length; i++) {
			System.out.println(Integer.toHexString(bytes[i]));
		}
		//正确的组装方法
		System.out.println("-------GBK--------");
		System.out.println(new String(bytes_copy, "utf-8"));
	}
	// GBK-->iso8859-1-->utf-8-->iso8859-1-->GBK
	public static void main4() throws Exception {
		
		//最开始是GBK
		bytes = zhongwen.getBytes("GBK");
		System.out.println("--------GBK----------");
		for (int i = 0; i < bytes.length; i++) {
			System.out.println(Integer.toHexString(bytes[i]));
		}
		
		//被组装成了iso8859-1,new出的字符串对象有问题,但是字节内容没变
		System.out.println("-------iso8859-1--------");
		zhongwen = new String(zhongwen.getBytes("GBK"), "iso8859-1");
		System.out.println(zhongwen);
		bytes = zhongwen.getBytes("iso8859-1");
		for (int i = 0; i < bytes.length; i++) {
			System.out.println(Integer.toHexString(bytes[i]));
		}
		//转换成utf-8在网络上传输,一个byte转换为两个字节,所以一共8个字节
		bytes = zhongwen.getBytes("utf-8");
		System.out.println("-------utf-8--------");
		for (int i = 0; i < bytes.length; i++) {
			System.out.println(Integer.toHexString(bytes[i]));
		}
		
		//server端接收到utf-8,首先组装成iso8859-1,new出的字符串对象有问题,显示4个问号
		System.out.println("-------server--iso8859-1------");
		zhongwen = new String(bytes, "utf-8");
		System.out.println(zhongwen);
		bytes = zhongwen.getBytes("iso8859-1");
		for (int i = 0; i < bytes.length; i++) {
			System.out.println(Integer.toHexString(bytes[i]));
		}
		
		//以iso8859-1得到字节,并组装成GBK
		System.out.println("-------GBK--------");
		System.out.println(new String(bytes, "GBK"));
	}



	public static void ____________________main() throws Exception {
		System.out.println(System.getProperty("file.encoding"));
		System.out.println(System.getProperty("user.language"));
		System.out.println(System.getProperty("user.region"));
	}

}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值