Unicode编码与中文互转
生活随笔
收集整理的這篇文章主要介紹了
Unicode编码与中文互转
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 /**
2 * unicode編碼轉換為漢字
3 * @param unicodeStr 待轉化的編碼
4 * @return 返回轉化后的漢子
5 */
6 public static String UnicodeToCN(String unicodeStr) {
7 Pattern pattern = Pattern.compile("(\\u(\p{XDigit}{4}))");
8 Matcher matcher = pattern.matcher(unicodeStr);
9 char ch;
10 while (matcher.find()) {
11 //group
12 String group = matcher.group(2);
13 //ch:'李四'
14 ch = (char) Integer.parseInt(group, 16);
15 //group1
16 String group1 = matcher.group(1);
17 unicodeStr = unicodeStr.replace(group1, ch + "");
18 }
19
20 return unicodeStr.replace("\", "").trim();
21 }
/**
* 漢字轉化為Unicode編碼
* @param CN 待轉化的中文
* @return 返回轉化之后的unicode編碼
*/
public static String CNToUnicode(String CN) {
try {
StringBuffer out = new StringBuffer("");
//直接獲取字符串的unicode二進制
byte[] bytes = CN.getBytes("unicode");
//然后將其byte轉換成對應的16進制表示即可
for (int i = 0; i < bytes.length - 1; i += 2) {
out.append("\u");
String str = Integer.toHexString(bytes[i + 1] & 0xff);
for (int j = str.length(); j < 2; j++) {
out.append("0");
}
String str1 = Integer.toHexString(bytes[i] & 0xff);
out.append(str1);
out.append(str);
}
return out.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
測試
1 public static void main(String[] args) {
2 String Unicodestr = "\u674e\u56db";
3 System.out.println("unicode為\u674e\u56db對應的中文是:"+Util.UnicodeToCN(Unicodestr));
4 String CNStr = "李四";
5 System.out.println("李四對應的Unicode編碼是:"+Util.CNToUnicode(CNStr));
6
7 }
測試結果:
這里可能需要解釋的是:ufeff。ufeff表示的是UTF-16(大端序)的編碼方式。在顯示的時候可以將ufeff過濾掉
總結
以上是生活随笔為你收集整理的Unicode编码与中文互转的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 生日吃饺子的寓意是什么
- 下一篇: 【模板】三分法