Java 如何把gb2312编码转化为汉字
程序员文章站
2022-03-31 19:14:33
...
public void testApp() throws Exception
{
String string = "C2ACD0E3cbd5343232373234313836323230323033313833";
String result = stringToGbk(string);
System.out.println(result);
}
//将gbk编码转换成汉字
public String stringToGbk(String string) throws Exception
{
byte[] bytes = new byte[string.length() / 2];
for(int i = 0; i < bytes.length; i ++){
byte high = Byte.parseByte(string.substring(i * 2, i * 2 + 1), 16);
byte low = Byte.parseByte(string.substring(i * 2 + 1, i * 2 + 2), 16);
bytes[i] = (byte) (high << 4 | low);
}
String result = new String(bytes, "gbk");
return result;
}
参考资料: