首页 > Java

Java中文字符串和Unicode编码互换

发表于2014-09-10 18:56:10| 976次阅读| 来源webkfa| 作者小豆豆

摘要:Java中文字符串和Unicode编码互换
java代码
01public class Test {
02 
03    public static void main(String[] args) {
04        // 把中文转成Unicode编码
05        System.out.println(gbEncoding("测试中文"));
06        // 把Unicode编码转成中文
07        String tt=decodeUnicode("\\u6d4b\\u8bd5\\u4e2d\\u6587");
08        System.out.println(tt);
09    }
10 
11    /**
12     * 把中文转成Unicode编码
13     *
14     * @param gbString
15     * @return
16     */
17    public static String gbEncoding(final String gbString) {
18        char[] utfBytes = gbString.toCharArray();
19        String unicodeBytes = "";
20        for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
21            String hexB = Integer.toHexString(utfBytes[byteIndex]);
22            if (hexB.length() <= 2) {
23                hexB = "00" + hexB;
24            }
25            unicodeBytes = unicodeBytes + "\\u" + hexB;
26        }
27        //System.out.println("unicodeBytes is: " + unicodeBytes);
28        return unicodeBytes;
29    }
30 
31    /**
32     * 把Unicode编码转成中文
33     *
34     * @param dataStr
35     * @return
36     */
37    public static String decodeUnicode(final String dataStr) {
38        int start = 0;
39        int end = 0;
40        final StringBuffer buffer = new StringBuffer();
41        while (start > -1) {
42            end = dataStr.indexOf("\\u", start + 2);
43            String charStr = "";
44            if (end == -1) {
45                charStr = dataStr.substring(start + 2, dataStr.length());
46            } else {
47                charStr = dataStr.substring(start + 2, end);
48            }
49            char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
50            buffer.append(new Character(letter).toString());
51            start = end;
52        }
53        return buffer.toString();
54    }
55 
56}

相关文章

猜你喜欢

学到老在线代码浏览器 关闭浏览
友情链接: hao123 360导航 搜狗网址导航 114啦网址导航 博客大全
Copyright © 1999-2014, WEBKFA.COM, All Rights Reserved  京ICP备14034497号-1