您的位置:首页 > 其它

字符编码

2016-05-09 15:26 288 查看
package 练习;
import java.io.UnsupportedEncodingException;
public class 编码 {
public static void main(String []args) throws UnsupportedEncodingException{
String string = "你好ABC";
byte[] byte1 = string.getBytes("utf-8");
//utf-8编码采用的汉字占三个字节,字母一个字节
for (byte b : byte1) {
System.out.print(Integer.toHexString(b & 0xff) + " ");
}
System.out.println();
byte[] byte2 = string.getBytes("utf-16be");
//java平台采用的双字节编码,全是占两个字节
for (byte b : byte2) {
System.out.print(Integer.toHexString(b & 0xff) + "  ");
}
System.out.println();
byte[] byte3 = string.getBytes("gbk");
//gbk编码中汉字占两个字节
for (byte b : byte3) {
System.out.print(Integer.toHexString(b & 0xff) + " ");
}
//二进制转换成字符串要采用相同的编码,否则会出现乱码
System.out.println("\n" + new String(byte1,"gbk"));
System.out.println(new String(byte1,"utf-8"));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: