使用下列其中一种方法并附带字符集参数来指定正确的字符集。
-
java.lang.String.String
(byte[],
java.lang.String)
-
java.lang.String.String
(byte[],
int,
int,
java.lang.String)
public static void main(String[] args) {
byte[] bytes = {(byte) 0xbc, (byte) 0xf2, (byte) 0xcc, (byte) 0xe5,
(byte) 0xd6, (byte) 0xd0, (byte) 0xce, (byte) 0xc4,};
String str = new String(bytes, "GB18030");
for (int i = 0, n = str.length(); i < n ; i++) {
System.out.println("0x" + Integer.toHexString(com.ibm.icu.text.UTF16.charAt(str, i)));
}
}
|
|
使用 java.nio 和 java.nio.charset 中的类
public static void main(String[] args) {
byte[] bytes = {
(byte) 0xbc,
(byte) 0xf2,
(byte) 0xcc,
(byte) 0xe5,
(byte) 0xd6,
(byte) 0xd0,
(byte) 0xce,
(byte) 0xc4,
};
Charset cs = Charset.forName("GB18030");
CharsetDecoder decoder = cs.newDecoder();
CharBuffer cb = decoder.decode(ByteBuffer.wrap(bytes));
String str = cb.toString();
for (int i = 0, n = str.length(); i < n ; i++) {
System.out.println("0x" + Integer.toHexString(com.ibm.icu.text.UTF16.charAt(str, i)));
}
}
|
|