Unicode 編碼比較

1 篇文章 / 0 new
author
Unicode 編碼比較
String str = "誠";
try {
    P.i(toHex(str, "UTF-8"));
    P.i(toHex(str, "UTF-16"));
    P.i(toHex(str, "UTF-16BE"));
    P.i(toHex(str, "UTF-16LE"));
    P.i(toHex(str, "UTF-32"));
    P.i(toHex(str, "UTF-32BE"));
    P.i(toHex(str, "UTF-32LE"));
} catch (UnsupportedEncodingException e) { }
 
private String toHex(String str, String code) throws UnsupportedEncodingException {
    byte[] b = str.getBytes(code);
    String buff = String.format("[%1$8s] 長度:%2$s", code, b.length);
    for(int i=0; i<b.length; i++) {
        buff += " "+String.format("%1$02x", b[i]).toUpperCase();
    }        
    return buff;
}
輸出結果
[   UTF-8] 長度:3 E8 AA A0
[  UTF-16] 長度:4 FF FE A0 8A
[UTF-16BE] 長度:2 8A A0
[UTF-16LE] 長度:2 A0 8A
[  UTF-32] 長度:8 FF FE 00 00 A0 8A 00 00
[UTF-32BE] 長度:4 00 00 8A A0
[UTF-32LE] 長度:4 A0 8A 00 00

資料若為 String str = "A";
輸出結果
[   UTF-8] 長度:1 41
[  UTF-16] 長度:4 FF FE 41 00
[UTF-16BE] 長度:2 00 41
[UTF-16LE] 長度:2 41 00
[  UTF-32] 長度:8 FF FE 00 00 41 00 00 00
[UTF-32BE] 長度:4 00 00 00 41
[UTF-32LE] 長度:4 41 00 00 00

 
資料若為 String str = "A誠";
輸出結果
[   UTF-8] 長度:4 41 E8 AA A0
[  UTF-16] 長度:6 FF FE 41 00 A0 8A
[UTF-16BE] 長度:4 00 41 8A A0
[UTF-16LE] 長度:4 41 00 A0 8A
[  UTF-32] 長度:12 FF FE 00 00 41 00 00 00 A0 8A 00 00
[UTF-32BE] 長度:8 00 00 00 41 00 00 8A A0
[UTF-32LE] 長度:8 41 00 00 00 A0 8A 00 00


 
Free Web Hosting