2
我試圖解碼使用字符·
字符集在java中GB2312java的字符集解碼問題
包含在GB2312這個字符,位置代碼a1a4
check here
代碼:
public static void main(String[] _args) throws Exception {
String str="a1a4:· a5f6:ヶ a8c5:ㄅ";
ByteBuffer bf=readToByteBuffer(new ByteArrayInputStream(str.getBytes()));
System.out.println(Charset.forName("GB2312").decode(bf).toString());
}
private static final int bufferSize = 0x20000;
static ByteBuffer readToByteBuffer(InputStream inStream) throws IOException {
byte[] buffer = new byte[bufferSize];
ByteArrayOutputStream outStream = new ByteArrayOutputStream(bufferSize);
int read;
while (true) {
read = inStream.read(buffer);
if (read == -1)
break;
outStream.write(buffer, 0, read);
}
ByteBuffer byteData = ByteBuffer.wrap(outStream.toByteArray());
return byteData;
}
以上輸出結果代碼爲:
a1a4:? a5f6:ヶ a8c5:ㄅ
我不明白爲什麼不能解碼a1a4
?
我假設'IO.string2InputStream(d)'也使用GB2312字符集寫入。你有沒有檢查緩衝區中的字節是否正確? – 2012-02-29 00:31:17
@RussellZahniser抱歉,編輯了我的問題。 – Koerr 2012-02-29 00:38:43
您可能想要執行'str.getBytes(「GB2312」)' - 您使用的是默認值,可能是UTF8。但是我認爲,呃關於它是一個人物問題而不是編碼問題是正確的。 – 2012-02-29 00:58:04