码迷,mamicode.com
首页 > 其他好文 > 详细

几种不同的字符编码方式

时间:2017-07-22 19:36:52      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:for   编译   编码   格式   填充   bytes   abc   16进制   字符编码   

1.gbk

gbk编码格式中一个中文占两个字节,英文占一个字节

String s = "你好ABC";
byte[] bytes = s.getBytes("gbk");
//gbk编码中文占两个字节,英文占一个字节
for (byte b : bytes) {
//把字节转化成int以16进制显示(只填充了int的低八位),与0xff(1111 1111)相与只取低八位。
System.out.print(Integer.toHexString(b & 0xff)+" ");
}

执行结果:

c4 e3 ba c3 41 42 43 

2.utf-8

byte[] bytes1 = s.getBytes("utf-8");
//utf-8中文占三个字节,英文占1个字节
for (byte b : bytes1) {
System.out.print(Integer.toHexString(b & 0xff)+" ");
}
System.out.println();

执行结果:

e4 bd a0 e5 a5 bd 41 42 43

3.utf-16be

//java是双字节编码utf-16be(编译形成class文件后),中文占两个字节,英文占两个字节
byte[] bytes2 = s.getBytes("utf-16be");
for (byte b : bytes2) {
System.out.print(Integer.toHexString(b & 0xff)+" ");
}

执行结果:

4f 60 59 7d 0 41 0 42 0 43 

 

几种不同的字符编码方式

标签:for   编译   编码   格式   填充   bytes   abc   16进制   字符编码   

原文地址:http://www.cnblogs.com/simple96/p/7222040.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!