1.使用
@Test
public void chineseToCode() throws UnsupportedEncodingException {
String str = URLEncoder.encode("大家好。", "UTF-8");
String[] s = str.substring(1).split("%");
StringBuffer s8 = new StringBuffer();
for(int i=0;i<s.length;i++){
s8.append("\\"+Integer.toOctalString(Integer.valueOf(s[i],16)));
}
System.out.println(s8.toString());
}
@Test
public void codeToChinese() throws UnsupportedEncodingException {
String code = "\\346\\227\\240\\350\\275\\246\\347\\211\\214";
String[] split = code.substring(1).split("\\\\");
StringBuffer s16 = new StringBuffer();
for (String s : split) {
s16.append("%"+Integer.toHexString(Integer.valueOf(s, 8)).toString().toUpperCase());
}
String decode = URLDecoder.decode(s16.toString(), "UTF-8");
System.out.println("decode = " + decode);
System.out.println(s16);
}
2.参考
反斜杠加3个数字是什么编码?JAVA转义序列、转义字符汇总。
java中二进制,八进制,十六进制之间的转换