import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UniCodeUtil {
// 中文字符串转UniCode
public static String getUNStr(String cnStr) {
char[] utfBytes = cnStr.toCharArray();
String unicodeBytes = "";
for (int i = 0; i < utfBytes.length; i++) {
String hexB = Integer.toHexString(utfBytes[i]);
if (hexB.length() <= 2) {
hexB = "00" + hexB;
}
unicodeBytes = unicodeBytes + "\\u" + hexB;
}
return unicodeBytes;
}
// JS写法:
// 中文转为Unicode
// toUnicode : function(s) {
// return s.replace(/([\u4E00-\u9FA5]|[\uFE30-\uFFA0])/g, function(newStr) {
// return "\\u" + newStr.charCodeAt(0).toString(16);
// });
// unicode转换为中文
public static String getCNStr(String unStr) {
if (unStr == null || unStr.length() == 0)
return "";
try {
String unicodeCompile = "(?<=\\\\u).{4}?";
String a;
Matcher matcher = Pattern.compile(unicodeCompile).matcher(unStr);
for (; matcher.find();) {
a = matcher.group();
String ch = String.valueOf((char) Integer.valueOf(a, 16)
.intValue());
unStr = unStr.replace("\\u" + a, ch);
}
return unStr;
} catch (Exception e) {
return unStr;
}
}
}