Unicode编码和中文互转(JAVA实现)
//中文转字符串
public static String gbEncoding(final String gbString) {//gbString = "测试"
char[] utfBytes = gbString.toCharArray();
//utfBytes = [测, 试]
String unicodeBytes = "";
for (int byteIndex = 0;
byteIndex < utfBytes.length;
byteIndex++) {
String hexB = Integer.toHexString(utfBytes[byteIndex]);
//转换为16进制整型字符串
if (hexB.length() <= 2) {
hexB = "00" + hexB;
}
unicodeBytes = unicodeBytes + "\\u" + hexB;
}
System.out.println("unicodeBytes is: " + unicodeBytes);
return unicodeBytes;
}
//Unicode转中文
public static String decodeUnicode(final String dataStr) {
int start = 0;
int end = 0;
final StringBuffer buffer = new StringBuffer();
while (start > -1) {
end = dataStr.indexOf("\\u", start + 2);
String charStr = "";
if (end == -1) {
charStr = dataStr.substring(start + 2, dataStr.length());
} else {
charStr = dataStr.substring(start + 2, end);
}
char letter = (char) Integer.parseInt(charStr, 16);
// 16进制parse整形字符串。
buffer.append(new Character(letter).toString());
start = end;
}
return buffer.toString();
【Unicode编码和中文互转(JAVA实现)】}
推荐阅读
- 急于表达——往往欲速则不达
- 第三节|第三节 快乐和幸福(12)
- 20170612时间和注意力开销记录
- 2.6|2.6 Photoshop操作步骤的撤消和重做 [Ps教程]
- 对称加密和非对称加密的区别
- 眼光要放高远
- 樱花雨
- 前任
- 2020-04-07vue中Axios的封装和API接口的管理
- 烦恼和幸福