posted by 셀로브 2013. 12. 19. 21:34


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class Unicode {

    public static String decode(String unicode)throws Exception {
        StringBuffer str = new StringBuffer();

        char ch = 0;
        forint i= unicode.indexOf("\\u"); i > -1; i = unicode.indexOf("\\u") ){
            ch = (char)Integer.parseInt( unicode.substring( i + 2, i + 6 ) ,16);
            str.append( unicode.substring(0, i) );
            str.append( String.valueOf(ch) );
            unicode = unicode.substring(i + 6);
        }
        str.append( unicode );

        return str.toString();
    }

    public static String encode(String unicode)throws Exception {
        StringBuffer str = new StringBuffer();

        for (int i = 0; i < unicode.length(); i++) {
            if(((int) unicode.charAt(i) == 32)) {
                str.append(" ");
                continue;
            }
            str.append("\\u");
            str.append(Integer.toHexString((int) unicode.charAt(i)));

        }

        return str.toString();

    }

    public static void main(String[] args) throws Exception {
        String str = encode("한 글");
        System.out.println(str);
        System.out.println(decode(str));
    }
}


'언어(Language) > JAVA' 카테고리의 다른 글

No enclosing instance of type 컴파일 오류  (0) 2013.12.19
Text File Read Write  (0) 2013.12.18
IS-A 관계와 HAS-A 관계  (0) 2013.10.20
캡슐화  (0) 2013.10.20
다형성  (0) 2013.10.20