深入解析java String中getBytes()的編碼問題

來源:互聯網
上載者:User

標籤:ref   []   app   print   static   問題   algo   odi   ted   

轉載請註明出處:http://www.cnblogs.com/Joanna-Yan/p/6900536.html 

Java伺服器後台在和Android端App通訊時,遇到了兩端關於用MD5加密同一包含中文的字串結果不一致的問題。

具體問題描述:

Java伺服器後台和Android端AS用了同一個MD5的工具類,且兩邊項目的預設編碼都是UTF-8 ,加密純英文數位字串時,結果一致,對同一包含中文的字串加密,發現結果不一樣,這是為什麼呢?

工具類MD5Util代碼如下:

public class MD5Util {    /**     * 將byte數組轉化為16進位輸出     * @param bytes     * @return     */    public static String convertByteToHexString(byte[] bytes){        String result="";        for (int i = 0; i < bytes.length; i++) {            int temp=bytes[i]&0xff;            String tempHex=Integer.toHexString(temp);            if(tempHex.length()<2){                result+="0"+tempHex;            }else{                result+=tempHex;            }        }        return result;    }        /**     * MD5加密     * @param message     * @return     * @throws UnsupportedEncodingException      */    public static String md5Jdk(String message) throws UnsupportedEncodingException{        String temp="";        try {            MessageDigest md5Digest=MessageDigest.getInstance("MD5");            byte[] encodeMD5Digest=md5Digest.digest(message.getBytes());            temp=convertByteToHexString(encodeMD5Digest);        } catch (NoSuchAlgorithmException e) {            e.printStackTrace();        }        return temp;    }}

 最後問題鎖定在:

IDE的項目預設編碼和平台運行環境的編碼不是一回事。

改成:message.getBytes("UTF-8");後能解決中文加密不一致的問題。

下面,就String的getBytes()方法深入瞭解下。

String的getBytes()方法是得到一個字串的位元組數組,但特別要注意的是,本方法將返回該作業系統預設的編碼格式的位元組數組。如果你在使用這個方法時不考慮這一點,你會發現在一個平台上運行良好的系統,放到另一台機器上會產生意想不到的問題。

比如下面的程式:

public class TestCharset {    public static void main(String[] args) {         new TestCharset().execute();     }         private void execute() {         String s = "Hello!你好!";         byte[] bytes = s.getBytes();        System.out.println("bytes lenght is:" + bytes.length);     } }

在一個中文WindowsXP系統下,運行時,結果為:

bytes lenght is:12

但是如果放到了一個英文的UNIX環境下運行:

$ java TestCharset bytes lenght is:9

如果你的程式依賴於該結果,將在後續操作中引起問題。為什麼在一個系統中結果為12,而在另外一個卻變成了9?上面已經提到了,該方法是和平台(編碼)相關的。

在中文作業系統中,getBytes方法返回的是一個GBK或GB2313的中文編碼的位元組數組,其中中文字元各佔兩個位元組。而在英文平台中,一般的預設編碼是“ISO-8859-1”,每個字元都只取一個位元組(而不管是否非拉丁字元)。

Java中的編碼支援

Java是支援多國編碼的,在Java中,字元都是以Unicode進行儲存的,比如,“你”字的Unicode編碼是“4f60”,我們可以通過下面的實驗代碼來驗證:

public class TestCharset {     public static void main(String[] args) {         char c = ‘你‘;         int i = c;         System.out.println(c);         System.out.println(i);     } }

不管你在任何平台上執行,都會有相同的輸出:

20320

20320就是Unicode “4f60”的整數值。其實,你可以反編譯上面的類,可以發現在生產的.calss檔案中字元“你”(或者其它任何中文字串)本身就是以Unicode編碼進行儲存的:

char c = ‘/u4F60‘; ... ...

即使你知道了編碼的編碼格式,比如:

javac -encoding GBK TestCharset.java

編譯後產生的.class檔案中仍然是以Unicode格式儲存中文字元或字串的。

所以。為了避免這種問題,建議大家都在編碼中使用String.getBytes(String charset)方法。

下面我們將從字串分別提取ISO-8859-1和GBK兩種編碼格式的位元組數組,看看會有什麼結果:

public class TestCharset {      public static void main(String[] args) {          new TestCharset().execute();      }        private void execute() {          String s = "Hello!你好!";          byte[] bytesISO8859 = null;          byte[] bytesGBK = null;          try {              bytesISO8859 = s.getBytes("iso-8859-1");              bytesGBK = s.getBytes("GBK");          } catch (java.io.UnsupportedEncodingException e) {              e.printStackTrace();          }          System.out.println("-------------- /n 8859 bytes:");          System.out.println("bytes is: " + arrayToString(bytesISO8859));          System.out.println("hex format is:" + encodeHex(bytesISO8859));          System.out.println();          System.out.println("-------------- /n GBK bytes:");          System.out.println("bytes is: " + arrayToString(bytesGBK));          System.out.println("hex format is:" + encodeHex(bytesGBK));      }        public static final String encodeHex(byte[] bytes) {          StringBuffer buff = new StringBuffer(bytes.length * 2);          String b;          for (int i = 0; i < bytes.length; i++) {              b = Integer.toHexString(bytes[i]);              // byte是兩個位元組的, 而上面的Integer.toHexString會把位元組擴充為4個位元組              buff.append(b.length() > 2 ? b.substring(6, 8) : b);              buff.append(" ");          }          return buff.toString();      }        public static final String arrayToString(byte[] bytes) {          StringBuffer buff = new StringBuffer();          for (int i = 0; i < bytes.length; i++) {              buff.append(bytes[i] + " ");          }          return buff.toString();      }  } 

執行結果:

-------------- /n 8859 bytes:bytes is: 72 101 108 108 111 33 63 63 63 hex format is:48 65 6c 6c 6f 21 3f 3f 3f -------------- /n GBK bytes:bytes is: 72 101 108 108 111 33 -60 -29 -70 -61 -93 -95 hex format is:48 65 6c 6c 6f 21 c4 e3 ba c3 a3 a1 

可見,在s中提取的8859-1格式的位元組數組長度為9,中文字元都變成了“63”,ASCII碼為63的是“?”,一些國外的程式在國內中文環境下運行時,經常會出現亂碼,上面布滿了“?”,就是因為編碼沒有進行正確處理的結果。

而提取的GBK編碼的位元組數組中正確得到了中文字元的GBK編碼。字元“你”、“好”、“!”的GBK編碼分別是:“c4e3”、“bac3”、“a3a1”。得到了正確的以GBK編碼的位元組數組,以後需要還原為中文字串時,可以使用下面方法:

new String(byte[] bytes, String charset)

 如果此文對您有協助,打賞我一下吧~ 

 

深入解析java String中getBytes()的編碼問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.