2 回答
TA贡献1856条经验 获得超5个赞
public static String GBK2UTF8(String gbkStr) {
try {
return new String(gbk2utf8(gbkStr), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
}
private static byte[] gbk2utf8(String gbkStr) {
int n = gbkStr.length();
byte[] utfBytes = new byte[3 * n];
int k = 0;
for (int i = 0; i < n; i++) {
int m = gbkStr.charAt(i);
if (m < 128 && m >= -128) {
utfBytes[k++] = (byte) m;
continue;
}
String word = Integer.toBinaryString(m);
StringBuilder sb = new StringBuilder();
int len = 16 - word.length();
sb.append("1110");
for (int j = 0; j < len; j++) {
sb.append("0");
}
sb.append(word);
sb.insert(8, "10");
sb.insert(16, "10");
String s1 = sb.substring(0, 8);
String s2 = sb.substring(8, 16);
String s3 = sb.substring(16);
byte b0 = Integer.valueOf(s1, 2).byteValue();
byte b1 = Integer.valueOf(s2, 2).byteValue();
byte b2 = Integer.valueOf(s3, 2).byteValue();
utfBytes[k++] = b0;
utfBytes[k++] = b1;
utfBytes[k++] = b2;
}
if (k < utfBytes.length) {
byte[] tmp = new byte[k];
System.arraycopy(utfBytes, 0, tmp, 0, k);
return tmp;
}
return utfBytes;
}
添加回答
举报