package com.imooc;
public class EncodeDeo {
public static void main(String[] args)throws Exception {// TODO Auto-generated method stubString 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