Unicode与中文转换工具类方法(转)

时间:2022-02-21 07:19:48
/*
* 中文转unicode编码
*/
public static String gbEncoding(final String gbString) {
char[] utfBytes = gbString.toCharArray();
String unicodeBytes = "";
for (int i = ; i < utfBytes.length; i++) {
String hexB = Integer.toHexString(utfBytes[i]);
if (hexB.length() <= ) {
hexB = "" + hexB;
}
unicodeBytes = unicodeBytes + "\\u" + hexB;
}
return unicodeBytes;
}
/*
* unicode编码转中文
*/
public static String decodeUnicode(final String dataStr) {
int start = ;
int end = ;
final StringBuffer buffer = new StringBuffer();
while (start > -) {
end = dataStr.indexOf("\\u", start + );
String charStr = "";
if (end == -) {
charStr = dataStr.substring(start + , dataStr.length());
} else {
charStr = dataStr.substring(start + , end);
}
char letter = (char) Integer.parseInt(charStr, ); // 16进制parse整形字符串。
buffer.append(new Character(letter).toString());
start = end;
}
return buffer.toString();
}

原文链接:https://www.cnblogs.com/boluoboluo/p/6504641.html