10진수
2진수, 8진수, 16진수
1. 10진수 → 2진수, 8진수, 16진수
Integer.toBinaryString , Integer.toOctalString , Integer.toHexString 를 사용한다.
public class NumberConvert {
public static void main(String[] args) {
int decimal = 10;
String binary = Integer.toBinaryString(decimal); // 10진수 -> 2진수
String octal = Integer.toOctalString(decimal); // 10진수 -> 8진수
String hexaDecimal = Integer.toHexString(decimal); // 10진수 -> 16진수
System.out.println("10진수 : " + decimal);
System.out.println("2진수 : " + binary);
System.out.println("8진수 : " + octal);
System.out.println("16진수 : " + hexaDecimal);
}
}
Java
복사
2. 2진수, 8진수, 16진수 → 10진수
parseInt(String s, int radix) 를 사용한다.
public class NumberConvert {
public static void main(String[] args) {
int binaryToDecimal = Integer.parseInt("1010", 2);
int octalToDecimal = Integer.parseInt("12", 8);
int hexaToDecimal = Integer.parseInt("A", 16);
System.out.println("2진수(1010) -> 10진수 : " + binaryToDecimal); // 10
System.out.println("8진수(12) -> 10진수 : " + octalToDecimal); // 10
System.out.println("16진수(a) -> 10진수 : " + hexaToDecimal); // 10
}
}
Java
복사
풀이코드
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040