java中unicode编码和utf8编码相互转换

时间:2022-12-27 14:06:35

1,在开发中经常会遇见有些编码使用到unicode编码的,而与我们日常使用的utf8编码不一致

2,代码

 /**
* <p>转为unicode 编码<p>
*
* @param str
* @return unicodeString
*/

public static String encode(String str) {
String prefix = "\\u";
StringBuffer sb = new StringBuffer();
char[] chars = str.toCharArray();
if (chars == null || chars.length == 0) {
return null;
}
for (char c : chars) {
sb.append(prefix);
sb.append(Integer.toHexString(c));
}
return sb.toString();
}

/**
* 把unicode编码转换为中文
*
* @param str
* @return
*/

public static String decode(String str) {
String sg = "\\u";
int a = 0;
List<String> list = new ArrayList<>();
while (str.contains(sg)) {
str = str.substring(2);
String substring;
if (str.contains(sg)) {
substring = str.substring(0, str.indexOf(sg));
} else {
substring = str;
}
if (str.contains(sg)) {
str = str.substring(str.indexOf(sg));
}
list.add(substring);
}
StringBuffer sb = new StringBuffer();
if (!CollectionUtils.isEmpty(list)){
for (String string : list) {
sb.append((char) Integer.parseInt(string, 16));
}
}
return sb.toString();
}

总结

其实把utf-8转换为unicode编码就是把每个字符转成16进制的字符串
反之,把16进制的字符串转为int数字之后强转位char