본문 바로가기
백엔드기술/개발언어

자바 UTF-8, EUC-KR 인코딩 , 디코딩

by RevFactory 2011. 9. 26.


CharCoding.java

package encodingtest;

import java.io.UnsupportedEncodingException;

/**
 *
 * @author Revfactory
 */
public class CharCoding {
   
    public static String encode(String str, String charset) {
        StringBuilder sb = new StringBuilder();
        try {
            byte[] key_source = str.getBytes(charset);
            for(byte b : key_source) {
                String hex = String.format("%02x", b).toUpperCase();
                sb.append("%");
                sb.append(hex);
            }
        } catch(UnsupportedEncodingException e) { }//Exception
        return sb.toString();
    }
   
    public static String decode(String hex, String charset) {
        byte[] bytes = new byte[hex.length()/3];
        int len = hex.length();
        for(int i = 0 ; i < len ;) {
            int pos = hex.substring(i).indexOf("%");
            if(pos == 0) {
                String hex_code = hex.substring(i+1, i+3);
                bytes[i/3] = (byte)Integer.parseInt(hex_code, 16);
                i += 3;
            } else {
                i += pos;
            }
        }
        try {
            return new String(bytes, charset);
        } catch(UnsupportedEncodingException e) { }//Exception
        return "";
    }
   
    public static String changeCharset(String str, String charset) {
        try {
            byte[] bytes = str.getBytes(charset);
            return new String(bytes, charset);
        } catch(UnsupportedEncodingException e) { }//Exception
        return "";
    }
}


EncodingTest.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package encodingtest;

/**
 *
 * @author Minho
 */
public class EncodingTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String source = "대한민국";
        System.out.println(source);
       
        String hex_utf8 = CharCoding.encode(source, "UTF-8");
        System.out.println(hex_utf8);       
        System.out.println(CharCoding.decode(hex_utf8, "UTF-8"));
       
        String hex_euckr = CharCoding.encode(source, "EUC-KR");
        System.out.println(hex_euckr);
        System.out.println(CharCoding.decode(hex_euckr, "EUC-KR"));
       
        System.out.println(CharCoding.changeCharset(source, "UTF-8"));
       
        System.out.println(CharCoding.changeCharset(source, "EUC-KR"));
    }
}



결과 :
대한민국
%EB%8C%80%ED%95%9C%EB%AF%BC%EA%B5%AD
대한민국
%B4%EB%C7%D1%B9%CE%B1%B9
대한민국
대한민국
대한민국



추가... 자바 API사용
public static void main(String[] args) {
        String str = "가나다라";
        try {
            String text = URLEncoder.encode(str, "UTF-8");
            String text2 = URLDecoder.decode(text, "UTF-8");
            System.out.println(text);
            System.out.println(text2);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(MyTest.class.getName()).log(Level.SEVERE, null, ex);
        }
}

결과
%EA%B0%80%EB%82%98%EB%8B%A4%EB%9D%BC
가나다라

'백엔드기술 > 개발언어' 카테고리의 다른 글

Java Date 클래스 활용하기  (0) 2011.10.10
한자를 한글로 변환하기  (4) 2010.07.22
임시 참조 - DB Connection  (0) 2010.05.25