1 package my.unicode; 2 3 import java.util.regex.Matcher; 4 import java.util.regex.Pattern; 5 6 public class UnicodeSwitchChinese 7 { 8 /** 9 * 10 * 轉:http://blog.csdn.net/z69183787/article/details/25742307 11 * 12 * 將字符串(不限於中文)轉換爲十六進制Unicode編碼字符串 13 */ 14 public static String stringToUnicode(String str) 15 { 16 str = (str == null ? "" : str); 17 String tmpStr = ""; 18 StringBuffer sb = new StringBuffer(1024); 19 char c; 20 int j = 0; 21 22 for (int i = 0; i < str.length(); i++) 23 { 24 c = str.charAt(i); 25 sb.append("\\u"); 26 27 j = (c >>> 8); //取出高8位 28 tmpStr = Integer.toHexString(j); 29 if (tmpStr.length() == 1) 30 { 31 sb.append("0"); 32 } 33 sb.append(tmpStr); 34 35 j = (c & 0xFF); //取出低8位 36 tmpStr = Integer.toHexString(j); 37 if (tmpStr.length() == 1) 38 { 39 sb.append("0"); 40 } 41 sb.append(tmpStr); 42 } 43 44 return (sb.toString()); 45 } 46 47 /* 48 * 把十六進制Unicode編碼字符串轉換爲中文字符串 49 */ 50 public static String unicodeToString(String str) 51 { 52 Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))"); 53 54 Matcher matcher = pattern.matcher(str); 55 56 char ch; 57 58 while (matcher.find()) 59 { 60 ch = (char) Integer.parseInt(matcher.group(2), 16); 61 62 str = str.replace(matcher.group(1), ch + ""); 63 } 64 65 return str; 66 67 } 68 69 public static void main(String[] args) 70 { 71 // 0x0075 和 \u0075 是等價的 72 System.out.println("0x0075:" + (char) 0x0075); 73 System.out.println("\\u0075:" + '\u0075'); 74 75 // 直接以Unicode字符串的方式初始化字符串時,會自動轉換爲漢子 76 String s1 = "\u005c\u005c\u0075\u662f\u6807\u8bc6\u0075\u006e\u0069\u0063\u006f\u0064\u0065\u7801\u7528\u7684\uff0c\u540e\u9762\u7684\u0034\u4f4d\u0031\u0036\u8fdb\u5236\u6570\u5219\u662f\u5bf9\u5e94\u5b57\u7b26\u7684\u0075\u006e\u0069\u0063\u006f\u0064\u0065\u7801"; 77 System.out.println("s1: " + s1);// 0x0075 78 79 // 轉換漢字爲Unicode碼 80 String s2 = "unicode編碼簡而言之就是將每個字符用16位2進制數標識。可是一般都用4位的16進制數標識。"; 81 s2 = UnicodeSwitchChinese.stringToUnicode(s2); 82 System.out.println("s2: " + s2); 83 84 // 轉換Unicode碼爲漢字 85 String s3 = UnicodeSwitchChinese.unicodeToString(s2); 86 87 System.out.println("s3: " + s3); 88 } 89 }
unicode 編碼在線轉換工具html
轉:http://www.cnblogs.com/skykang/archive/2011/06/02/2068802.htmljava
中文字符 與 十六進制Unicode編碼 相互轉換(JavaScript)git
轉:http://www.cnblogs.com/duanhuajian/archive/2013/04/26/3045144.htmloracle
POI:app