您的位置:首页 > 编程语言 > Java开发

java中编码

2016-03-19 15:46 579 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: