java中编码

来源:互联网 发布:cf总是网络异常 编辑:程序博客网 时间:2024/06/03 18:15
package com.imooc;

public class EncodeDeo {

public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
String s="慕课ABC";
byte[] bytes1=s.getBytes();
for (byte b : bytes1) {
//把字节转换成了int,以16进制的方式显示
System.out.print(Integer.toHexString(b&0xff)+" ");
}
System.out.println();
byte[] bytes2=s.getBytes("gbk");
for (byte b2 : bytes2) {
System.out.print(Integer.toHexString(b2&0xff)+" ");
}
System.out.println();
byte[] bytes3=s.getBytes("utf-8");
for (byte b2 : bytes3) {
System.out.print(Integer.toHexString(b2&0xff)+" ");
}
System.out.println();
byte[] bytes4=s.getBytes("utf-16be");
for (byte b2 : bytes4) {
System.out.print(Integer.toHexString(b2&0xff)+" ");
}
System.out.println();
/**
* 当你的字节序列是某种编码时,这个时候想把字节变成字符串,也需要这种编
* 码方式,否则会出现乱码
*/
//用项目默认的编码
String str1=new String(bytes4);
System.out.println(str1);
String str2=new String(bytes4,"utf-16be");
System.out.println(str2);
}

}

运行结果为

c4 bd bf ce 41 42 43 c4 bd bf ce 41 42 43 e6 85 95 e8 af be 41 42 43 61 55 8b fe 0 41 0 42 0 43 aU孇

慕课ABC

0 0
原创粉丝点击