Java
10진수를 2진수, 8진수, 16진수로 변환
jayden-lee
2019. 4. 7. 21:55
728x90
소스 코드
public class Main {
public static void main(String[] args) {
// 10진수
int num = 120;
String decimalToBinary = Integer.toBinaryString(num);
String decimalToOctal = Integer.toOctalString(num);
String decimalToHex = Integer.toHexString(num);
System.out.println("10진수 -> 2진수, 8진수, 16진수로 변환");
System.out.println("10진수: " + num + ", 2진수: " + decimalToBinary);
System.out.println("10진수: " + num + ", 8진수: " + decimalToOctal);
System.out.println("10진수: " + num + ", 16진수: " + decimalToHex);
System.out.println();
System.out.println("2진수, 8진수, 16진수를 10진수로 변환");
System.out.println("2진수: " + decimalToBinary + ", 10진수: " + Integer.parseInt(decimalToBinary, 2));
System.out.println("8진수: " + decimalToOctal + ", 10진수: " + Integer.parseInt(decimalToOctal, 8));
System.out.println("16진수: " + decimalToHex + ", 10진수: " + Integer.parseInt(decimalToHex, 16));
}
}