4
我發現了在Java中將字節轉換爲UTF8字符串的奇怪問題。 爲什麼字節1和字節2是不同的,但是str1和srt2是相同的?如何從java中的不同字節數據獲取相同的字符串?
這裏是測試代碼。
import org.apache.commons.codec.binary.Hex;
public class MyTest {
public static void main(String[] args) throws Exception {
byte[] bytes1 = Hex.decodeHex("EDA0BDEDB88A".toCharArray());
byte[] bytes2 = Hex.decodeHex("F09F988A".toCharArray());
System.out.println("bytes1 length: " + bytes1.length);
System.out.println("bytes2 length: " + bytes2.length);
String str1 = new String(bytes1, "utf8");
String str2 = new String(bytes2, "utf8");
System.out.println("str1 is equals str2? " + str1.equals(str2));
}
}
這裏是JDK7
bytes1 length: 6
bytes2 length: 4
str1 is equals str2? true
運行測試代碼的輸出是否有可能找出「EDA0BDEDB88A」和「F09F988A」之間的關係?
'F09F988A'是unicode smail face,但'EDA0BDEDB88A'未知。