2015-02-08 38 views
0

我有一個包含整數值的數組列表。使用Intege將ArrayList轉換爲UTF-16字符串

看起來像83 0 97 0 109 0 112 0 108 0 101 0

而且我知道它是UTF-16編碼。

所以我想將其改造成字符串Sample

目前,當我忽略空值和剩餘使用:

String.valueOf(Character.toChars(codePoint)); 

這將導致不正確編碼非英文字母。

有誰知道如何將這樣的ArrayList轉換爲正確的UTF-16字符串?

+0

您正在使用BOM – pixel 2015-02-08 19:30:58

+0

這些整數值代表什麼? – 2015-02-08 19:34:20

+0

@SotiriosDelimanolis代碼點 – pixel 2015-02-08 19:36:08

回答

3
public String fromUTF16LE(int... bytes) { 
    byte[] b = new byte[bytes.length]; 
    for (int i = 0; i < b.length; ++) { 
     b[i] = (byte) bytes[i]; 
    } 
    return new String(b, StandardCharsets.UTF_16LE); 
} 

很明顯,整數是字節,以UTF-16的小尾序排列。 有人可能會檢查大小是否均勻。 解碼器可能用於錯誤處理。