
Udemy강의 중 자바 강의를 수강하며 정리한 내용이다.
Java에는 진수 표현 방식이 있다.
16진수
a. 0-15까지를 표현할수 있다.
b. 0-9는 숫자로, 10-15까지는 알파벳 순서대로 A-F로 표현한다.
c. 0x를 붙여준다. (ex - 0xf = 15를 의미함.)
public static void main(String[] args) {
int x = 0xf;
System.out.println(x);
}
결과 : 15
10진수
a. 그냥 숫자 쓰면 된다.
b. 0-9까지의 수를 표현할 수 있다.
public static void main(String[] args) {
int x = 10;
System.out.println(x);
}
결과 : 10
8진수
a. 0-7까지의 수를 표현한다.
b. 앞에 0을 붙인다. (ex - 0100 = 64를 의미함)
public static void main(String[] args) {
int x = 0100;
System.out.println(x);
}
결과 : 64
2진수
a. 2진수는 0과 1로 수를 표현한다.
b. 0b를 붙인다. (ex - 0b1001 = 9를 의미함.)
public static void main(String[] args) {
int x = 0b1001;
System.out.println(x);
}
결과 : 9
Integer클래스의 함수를 사용하여 할 수도 있다.
하지만 이건 String으로 변환하는 거라는 것을 유의하자.
16 : Interger.toHexString(int x)
8 : Interger.toOctalString(int x)
2 : Interger.toBinaryString(int x)
public static void main(String[] args) {
int x = 10;
System.out.println(Integer.toHexString(x));
System.out.println(Integer.toOctalString(x));
System.out.println(Integer.toBinaryString(x));
}
결과 :
a
12
1010