1 import org.apache.commons.codec.binary.Hex;
2 import org.junit.Test;
3
4 /**
5 * HBASE中文转换
6 */
7 public class CHCode {
8
9 /**
10 * 中文转utf8
11 */
12 @Test
13 public void testStr2UTF8() throws Exception {
14 String str = "烦";
15 char[] chars = Hex.encodeHex(str.getBytes("UTF-8"));
16 System.out.println(chars);
17 }
18
19 /**
20 * utf8转中文
21 */
22 @Test
23 public void testUTF8ToStr() throws Exception {
24 // String str = new String(Hex.decodeHex("e783a6e783a6e783a6".toCharArray()), "UTF-8");
25
26 byte[] bytes = Hex.decodeHex("e783a6e783a6e783a6".toCharArray());
27 String str = new String(bytes, "UTF-8");
28
29 System.out.println("bytes:" + bytes);
30 System.out.println("String:" + str);
31 }
32
33 /**
34 * HBASE中文转换
35 */
36 @Test
37 public void testHbaseStr() throws Exception {
38 // Hbase UTF8编码
39 String content = "\\xE7\\x83\\xA6";
40 char[] chars = content.toCharArray();
41 StringBuffer sb = new StringBuffer();
42 for (int i = 2; i < chars.length; i = i + 4) {
43 // System.out.println(chars[i]);
44 sb.append(chars[i]);
45 // System.out.println(chars[i + 1]);
46 sb.append(chars[i + 1]);
47 }
48 System.out.println(sb);
49 String ouputStr = new String(Hex.decodeHex(sb.toString().toCharArray()), "UTF-8");
50 System.out.println(ouputStr);
51 }
52
53
54 }