반응형

이기종의 플랫폼과 연동할 이슈가 발생하면, 네트웍을 통한 Byte Ordering과 더불어 스트링의 인코딩/디코딩도 이슈거리입니다.  아래는 자바 API 5의 java.nio.charset.Charset 에 나오는 내용입니다.

표준 캐릭터셋
Java 플랫폼의 구현은 모두 다음의 표준 캐릭터셋를 지원 할 필요가 있습니다. 지원 되고 있는 그 외의 캐릭터셋에 대해서는 구현의 릴리스 노트를 참조하십시오. 그러한 옵션의 캐릭터셋의 동작은 구현 마다 다를 가능성이 있습니다.

캐릭터셋           설명
US-ASCII          7 비트 ASCII (ISO646-US/Unicode 캐릭터셋의 Basic Latin 블록)
ISO-8859-1      ISO Latin Alphabet No. 1 (ISO-LATIN-1)
UTF-8                8 비트 UCS 변환 형식
UTF-16BE        16 비트 UCS 변환 형식, 빅 endian 바이트순서
UTF-16BE        16 비트 UCS 변환 형식, little endian 바이트순서
UTF-16             16 비트 UCS 변환 형식, 옵션의 바이트순서 마크로 식별되는 바이트순서

아래코드는 예제입니다.
class CharSetTest {

    static String aa = "a1";

    static void testAscii() {
        try {
            byte[] bytes = aa.getBytes("US-ASCII");
            for(int i=0; i < bytes.length; i++) {
                System.out.print(bytes[i]);
            }

            System.out.println("");
        }catch(java.io.UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    static void testUTF8() {
        try {
            byte[] bytes = aa.getBytes("UTF-8");
            for(int i=0; i < bytes.length; i++) {
                System.out.print(bytes[i]);
            }

            System.out.println("");
        }catch(java.io.UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    static void testUTF16() {
        try {
            byte[] bytes = aa.getBytes("UTF-16");
            for(int i=0; i < bytes.length; i++) {
                System.out.print(bytes[i]);
            }

            System.out.println("");
        }catch(java.io.UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    static void testUTF16BE() {
        try {
            byte[] bytes = aa.getBytes("UTF-16BE");
            for(int i=0; i < bytes.length; i++) {
                System.out.print(bytes[i]);
            }

            System.out.println("");
        }catch(java.io.UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    static void testUTF16LE() {
        try {
            byte[] bytes = aa.getBytes("UTF-16LE");
            for(int i=0; i < bytes.length; i++) {
                System.out.print(bytes[i]);
            }

            System.out.println("");
        }catch(java.io.UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println("--- Ascii ---");
        CharSetTest.testAscii();
        System.out.println("--- UTF-8 ---");
        CharSetTest.testUTF8();
        System.out.println("--- UTF-16 ---");
        CharSetTest.testUTF16();
        System.out.println("--- UTF-16BE ---");
        CharSetTest.testUTF16BE();
        System.out.println("--- UTF-16LE ---");
        CharSetTest.testUTF16LE();
    }
}
아래 예는 화면결과 입니다.
--- Ascii ---
9749
--- UTF-8 ---
9749
--- UTF-16 ---
-2-1097049
--- UTF-16BE ---
097049
--- UTF-16LE ---
970490
< 출처: http://www.sjava.net/62 >

+ Recent posts