문자 인코딩(Character encoding, Text encoding)
- 인코딩은 문자를 고유한 숫자로 매핑한 규약일 뿐이며, 특정 진법에 종속되지 않는다
- ASCII(American Standard Code for Information Interchange)
- Unicode
문자 |
ASCII |
Unicode |
0~9 |
48~57 |
U+0030 ~ U+0039 |
A~Z |
65~90 |
U+0041 ~ U+005A |
a~z |
97~122 |
U+0061 ~ U+007A |
문자 ↔︎ 숫자 변환
// 문자형 '3'을 숫자 3으로 변환
char three = '3';
int a = three - '0';// 3
// 숫자 3을 문자형 '3'으로 변환
char charThree = a + '0';// '3'
- '3'을 숫자로 변환하려면
(int) ('3' - '0') = 3
- 3을 문자로 변환하려면
(char) (3 + '0') = '3'
Char 자료형에서 대문자 ↔︎ 소문자 변환
System.out.println((int) 'a');// 97
System.out.println((int) 'A');// 65
// 1. 대문자와 소문자의 10진수 값 차이: 32
int difference = 'a' - 'A';// 97 - 65 = 32
// 2. 대소문자 값 차이를 이용해서 대소문자 변경 가능
char origin = 'c';
char upperCase = (char) (lowerCase - difference);
char lowerCase = (char) (upperCase + difference);
대문자 = 소문자 - 32
소문자 = 대문자 + 32
- 연산 결과는
int
형 이므로 char
변수에 저장하려면 강제 형 변환 필요