Unicode配置文件有一段
abc\u4e13\u4e1312ab\u4e1a\u4ee3\u7406
对应的汉字值为:
abc专业12ab代理
自己写了几行代码,进行了转换,使用正则表达式匹配替换
// 完成Unicode转换为字符串
public static String unicodeToString(String str) {
Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
Matcher matcher = pattern.matcher(str);
char ch;
while (matcher.find()) {
ch = (char) Integer.parseInt(matcher.group(2), 16);
str = str.replace(matcher.group(1), ch + "");
}
return str;
}
汉字转为Unicode
// 将中文转换为Unicode编码
public static String stringToUnicode(String s) {
String str = "";
for (int i = 0; i < s.length(); i++) {
int ch = (int) s.charAt(i);
if (ch > 255)
// toHexString() 方法返回为无符号整数基数为16的整数参数的字符串表示形式
str += "\\u" + Integer.toHexString(ch);
else
str += "\\" + Integer.toHexString(ch);
}
return str;
}