-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncodingMain2.java
More file actions
61 lines (48 loc) · 2.26 KB
/
Copy pathEncodingMain2.java
File metadata and controls
61 lines (48 loc) · 2.26 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package charset;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import static java.nio.charset.StandardCharsets.*;
public class EncodingMain2 {
private static final Charset EUC_KR = Charset.forName("EUC_KR");
private static final Charset MS_949 = Charset.forName("MS_949");
public static void main(String[] args) {
System.out.println("== 영문 ASCII 인코딩 ==");
test("A", US_ASCII, US_ASCII);
test("A", US_ASCII, ISO_8859_1); // ASCII 확장 (LATIN-1)
test("A", US_ASCII, EUC_KR); // ASCII 포함
test("A", US_ASCII, MS_949); // ASCII 포함
test("A", US_ASCII, UTF_8); // ASCII 포함
test("A", US_ASCII, UTF_16BE); // UTF_16 디코딩 실패
System.out.println("== 한글 인코딩 - 기본 ==");
test("가", US_ASCII, US_ASCII); // X
test("가", ISO_8859_1, ISO_8859_1); // X
test("가", EUC_KR, EUC_KR); // O
test("가", MS_949, MS_949); // O
test("가", UTF_8, UTF_8); // O
test("가", UTF_16BE, UTF_16BE); // O
System.out.println("== 한글 인코딩 - 복잡한 문자 ==");
test("뷁", EUC_KR, EUC_KR); // X
test("뷁", MS_949, MS_949); // O
test("뷁", UTF_8, UTF_8); // O
test("뷁", UTF_16BE, UTF_16BE); // O
System.out.println("== 한글 인코딩 - 디코딩이 다른 경우 ==");
test("가", EUC_KR, MS_949); // O
test("뷁", MS_949, EUC_KR); // 인코딩 가능, 디코딩 x
test("가", EUC_KR, UTF_8); // X
test("가", MS_949, UTF_8); // X
test("가", UTF_8, MS_949 ); // X
System.out.println("== 영문 인코딩 - 디코딩이 다른 경우 ==");
test("A", EUC_KR, UTF_8); // O
test("A", MS_949, UTF_8); // O
test("A", UTF_8, MS_949); // O
test("A", UTF_8, UTF_16); // X
}
public static void test(String text, Charset encoding, Charset decoding){
byte[] encoded = text.getBytes(encoding);
String decoded = new String(encoded, decoding);
System.out.printf("%s -> [%s]인코딩 -> %s %sbyte -> [%s] 디코딩 -> %s\n ",
text, encoding, Arrays.toString(encoded), encoded.length,
decoding, decoded);
}
}