為什么我的代碼用什么方式輸出都是一樣的
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//項目默認(rèn)的是gbk編碼格式
//utf-8 漢字占用三個字節(jié) 字母占一個字節(jié) gbk 中文占兩個字節(jié) 英文占一個字節(jié) ?utf-16be 中文占兩個字節(jié) 英文占兩個字節(jié)
? ? ? ? ?String s="墨客ADC";
? ? ? ? ?byte[] bytes=s.getBytes("utf-8");
? ? ? ? ?for(byte b:bytes){
? ? ? ? System.out.print(Integer.toHexString(b & 0xff)+" ?");
? ? ? ? ?}
? ? ? ? System.out.println("");
? ? ? ? ?
? ? ? ? byte[] byte2=s.getBytes();
? ? ? ? ? ? ?for(byte b2:bytes){
? ? ? ? ? ? System.out.print(Integer.toHexString(b2 & 0xff)+" ?");
? ? ? ? ?}
? ? ? ? ? ? ?System.out.println();
? ? ? ? ?
? ? ? ? ? ? ?byte[] byte3=s.getBytes("gbk");
? ? ? ? ? ? ?for(byte b3:bytes){
? ? ? ? ? ? System.out.print(Integer.toHexString(b3 & 0xff)+" ?");
? ? ? ? ? ? ?}
? ? ? ? ? ? ?System.out.println();
? ? ? ? ? ? ?
? ? ? ? ? ? ?byte[] byte4=s.getBytes("gbk");
? ? ? ? ? ? ?for(byte b4:bytes){
? ? ? ? ? ? System.out.print(Integer.toHexString(b4 & 0xff)+" ?");
? ? ? ? ? ? ?}
? ? ? ? ? ? ?/*
? ? ? ? ? ? ? * 把字符轉(zhuǎn)換成字符串輸出 用什么方式編寫的字符用什么方式解析
? ? ? ? ? ? ? */
? ? ? ? ? ? ?String a=new String(byte2);
? ? ? ? ? ? ?System.out.println(a);
? ? ? ? ? ? ?String ab=new String(bytes,"utf-8");
? ? ? ? ? ? ?System.out.println(ab);
}
2017-04-18
大哥 ?你遍歷都是同一個 也就是第一個 ??for(byte b3:bytes){} ?for(byte b2:bytes){} ??for(byte b:bytes){}