博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

中文字符串转换为十六进制Unicode编码字符串

Posted on 2015-10-11 20:31  Co7Co7  阅读(3882)  评论(0编辑  收藏  举报

 

 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 }
View Code

 

unicode 编码在线转换工具

转:http://www.cnblogs.com/skykang/archive/2011/06/02/2068802.html

 

中文字符 与 十六进制Unicode编码 相互转(JavaScript)

转:http://www.cnblogs.com/duanhuajian/archive/2013/04/26/3045144.html

 

POI:

转:https://community.oracle.com/docs/DOC-887397

转:http://blog.csdn.net/spp_1987/article/details/13770637