반응형
Java에서는 숫자의 진법 변환을 매우 간단하게 처리할 수 있는 내장 메서드를 제공합니다.
특히, 8진수, 10진수, 16진수와 같은 다양한 진법을 다룰 때 효율적입니다.
이번 글에서는 Integer 클래스의 주요 메서드들을 활용해 진법 변환을 정리하겠습니다.
1. Integer.parseInt(String s, int radix)
parseInt 메서드는 문자열로 주어진 숫자를 지정된 진법(기수)으로 해석하여 10진수 정수로 변환합니다.
사용법
int decimal = Integer.parseInt("17", 8); // 8진수 '17'을 10진수로 변환
System.out.println(decimal); // 출력: 15
매개변수
- s: 변환하려는 숫자를 나타내는 문자열.
- radix: 숫자가 표현된 진법 (2, 8, 10, 16 등).
예제
int decimalFromOctal = Integer.parseInt("17", 8); // 8진수 -> 10진수
int decimalFromBinary = Integer.parseInt("101", 2); // 2진수 -> 10진수
int decimalFromHex = Integer.parseInt("1A", 16); // 16진수 -> 10진수
System.out.println(decimalFromOctal); // 출력: 15
System.out.println(decimalFromBinary); // 출력: 5
System.out.println(decimalFromHex); // 출력: 26
2. Integer.toBinaryString(int i)
toBinaryString 메서드는 주어진 10진수 정수를 2진수 문자열로 변환합니다.
사용법
String binary = Integer.toBinaryString(15); // 10진수 '15'를 2진수로 변환
System.out.println(binary); // 출력: 1111
예제
System.out.println(Integer.toBinaryString(15)); // 출력: 1111
System.out.println(Integer.toBinaryString(5)); // 출력: 101
3. Integer.toOctalString(int i)
toOctalString 메서드는 주어진 10진수 정수를 8진수 문자열로 변환합니다.
사용법
String octal = Integer.toOctalString(15); // 10진수 '15'를 8진수로 변환
System.out.println(octal); // 출력: 17
예제
System.out.println(Integer.toOctalString(15)); // 출력: 17
System.out.println(Integer.toOctalString(8)); // 출력: 10
4. Integer.toHexString(int i)
toHexString 메서드는 주어진 10진수 정수를 16진수 문자열로 변환합니다.
사용법
String hex = Integer.toHexString(26); // 10진수 '26'을 16진수로 변환
System.out.println(hex); // 출력: 1a
예제
System.out.println(Integer.toHexString(26)); // 출력: 1a
System.out.println(Integer.toHexString(255)); // 출력: ff
5. 진법 변환을 조합한 예제
8진수를 2진수로 변환
String octal = "17"; // 8진수 문자열
int decimal = Integer.parseInt(octal, 8); // 8진수 -> 10진수
String binary = Integer.toBinaryString(decimal); // 10진수 -> 2진수
System.out.println(binary); // 출력: 1111
16진수를 2진수로 변환
String hex = "1A"; // 16진수 문자열
int decimal = Integer.parseInt(hex, 16); // 16진수 -> 10진수
String binary = Integer.toBinaryString(decimal); // 10진수 -> 2진수
System.out.println(binary); // 출력: 11010
6. 활용 예제: 모든 진법 변환 프로그램
아래는 입력받은 숫자를 다양한 진법으로 변환하는 프로그램입니다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number and its base (e.g., '17 8' for octal):");
String number = scanner.next();
int base = scanner.nextInt();
// 입력받은 숫자를 10진수로 변환
int decimal = Integer.parseInt(number, base);
// 다른 진법으로 변환
System.out.println("Decimal: " + decimal);
System.out.println("Binary: " + Integer.toBinaryString(decimal));
System.out.println("Octal: " + Integer.toOctalString(decimal));
System.out.println("Hexadecimal: " + Integer.toHexString(decimal));
}
}
BigInteger 구현 로직
Java에서는 BigInteger 클래스를 사용하면 매우 큰 숫자(333,334자리 이상도 가능)를 정확히 처리할 수 있습니다.
BigInteger는 내부적으로 문자열을 사용해 계산하므로 정수형(int, long)의 크기 제한을 넘는 입력도 처리할 수 있습니다.
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 8진수 입력받기 (문자열 형태)
String octal = scanner.next();
// 8진수 -> 10진수로 변환
BigInteger decimal = new BigInteger(octal, 8);
// 10진수 -> 2진수로 변환
String binary = decimal.toString(2);
// 결과 출력
System.out.println(binary);
}
}
반응형
'Java' 카테고리의 다른 글
Java | JIT 컴파일러에 대해 (0) | 2024.12.18 |
---|---|
Java | HashSet이란? HashSet을 사용하기 좋은 상황(CT) (1) | 2024.12.09 |
Java | 'a'란 무엇인가? (0) | 2024.12.09 |
JAVA | JVM(Java Virtual Machine)이란? JVM의 작동원리 (2) | 2024.08.21 |
Java | 정적 팩토리 메서드(Static Factory Method) (0) | 2024.03.04 |