计算机中编码转换

代码1

public class BytesCodeTest {

public static void main(String[] args) {
	String test = "安徽省zx";
	System.out.print("原始字符:");
	for (char b : test.toCharArray()) {
		System.out.print(b + " ");
	}
	System.out.println();

	try {
		byte[] iso8859 = test.getBytes("ISO-8859-1");
		printByte("ISO-8859-1", iso8859);

		byte[] gb2312 = test.getBytes("GB2312");
		printByte("GB2312", gb2312);

		byte[] gbk = test.getBytes("GBK");
		printByte("GBK", gbk);

		byte[] utf8 = test.getBytes("UTF-8");
		printByte("UTF-8", utf8);

		byte[] utf16 = test.getBytes("UTF-16");
		printByte("UTF-16", utf16);
	} catch (Exception e) {
		e.printStackTrace();
	}
}

public static void printByte(String charset, byte[] bytes) {
	System.out.print("编码格式【" + charset + "]时  bytes= ");
	for (byte b : bytes) {
		System.out.print(b + " ");
	}
	System.out.println();
}

}

程序运行结果:

输入图片说明

结论:

  1. 1、英文字母在以上编码格式中均是一个byte,且对应的byte值均相同;
  2. 2、ISO-8859-1的编码格式中一个字符一个byte;
  3. 3、GBK和GB2312的编码结果基本上相同;
  4. 4、UTF-8编码格式中一个中文字符为三个byte,英文字符是一个byte;
  5. 5、UTF-16编码格式中每个字符均是2个byte。******
这里输入代码
```public class BytesCodeTranslate {
	public static void main(String[] args) throws UnsupportedEncodingException {
		String strGBK = "订单号重复";
		System.out.print("编码格式为【GBK】下的bytes: ");
		for (byte bt : strGBK.getBytes("GBK")) {
			System.out.print(bt + " ");
		}
		System.out.println();
		System.out.print("编码格式为【UTF-8】下的bytes: ");
		String strUTF8 = new String(strGBK.getBytes("UTF-8"), "UTF-8");
		for (byte bt : strUTF8.getBytes("UTF-8")) {
			System.out.print(bt + " ");
		}
		System.out.println();
		System.out.println("GBK-->UTF-8" + strUTF8);
		System.out.println("UTF-8-->GBK" + new String(strUTF8.getBytes("GBK"), "GBK"));
	}
}

转载于:https://my.oschina.net/u/3545495/blog/1550326

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值