So, I have a char [] array containing text and other data.
How to extract Chinese text from char [] array? Right now i can get english using
public String getString(int index, int length) { String str = ""; for (int i = 0; i < length && this.data[index + i] != 0; i++) str = str + this.data[index + i]; return str; }
then I try this:
try { String charset = "GB18030"; String str = new String(m.target.getBytes("UTF-16"), "GB18030"); System.out.println(str); System.out.println(str.equals("大家")); } catch (UnsupportedEncodingException e) {
m.target is the string I got from the byte [] array using getString () above. I tried various encodings and their combinations, and none of them displays the text correctly (大家), and no one returns true for str.equals ("大家")
EDIT
Using this method, I can successfully get Chinese characters.
public String test(int index, int length) { byte[] t = new byte[this.data.length]; for (int i = 0; i < this.data.length; i++) t[i] = (byte) this.data[i]; try { return new String(t, index, length, "GB18030"); } catch (UnsupportedEncodingException e) {
But now my question is ... I thought the maximum byte could be 127? How can an array of bytes contain high byte characters? Can I safely change the buffer to byte [] instead of char []?
source share