Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 백준 #
- 프로그래머스 #카테고리 별 도서 판매량 집계하기 #알고리즘 #쿼리문 #MySQL #코린이 #개발자 #study #취준
- 프로그래머스 #조건에 맞게 수열 변환하기 1 #알고리즘 #자바 #Java #코린이 #개발자 #study #취준
- 프로그래머스 #날짜 비교하기 #알고리즘 #자바 #Java #코린이 #개발자 #study #공부 #성장
- 프로그래머스 #배열의 원소만큼 추가하기 #알고리즘 #코린이 #개발자 #study #취준
- 프로그래머스 #x 사이의 개수 #알고리즘 #자바 #Java #코린이 #개발자 #study
- 프로그래머스 #알고리즘 #ad 제거하기 #Java #자바 #코린이 #개발자 #study #취준
- 프로그래머스 #합성수 찾기 #알고리즘 #Java #자바 #stiudy #코린이 #개발자 #공부 #성장
- 프로그래머스 #배열 회전시키기 #알고리즘 #Java #자바 #코린이 #개발자 #study #취준
- 프로그래머스 #배열에서 문자열 대소문자 변환하기 #알고리즘 #Java #자바 #코린이 #개발자 #study #취준
- 코린이 #개발자 #study
- 프로그래머스 #할 일 목록 #알고리즘 #Java #자바 #코린이 #개발자 #study #취준
- 프로그래머스 #1로 만들기 #알고리즘 #자바 #Java #코린이 #개발자 #study #공부 #성장
- 프로그래머스 #수열과 구간 쿼리1 #알고리즘 #자바 #Java #코린이 #개발자 #study #공부 #성장
- 프로그래머스 #길이에 따른 연산 #알고리즘 #자바 #Java #코린이 #개발자 #study #취준
- 프로그래머스 #오랜 기간 보호한 동물(1) #알고리즘 #MySQL #DB #쿼리문 #개발자 #study #성장 #코린이
- 프로그래머스 #푸드 파이트 대회 #알고리즘 #Java #자바 #코린이 #개발자 #study #취준
- 프로그래머스 #세로 읽기 #알고리즘 #개발자 #코린이 #study #성장 #Java #자바
- 프로그래머스 #간단한 식 계산하기 #알고리즘 #Java #자바 #코린이 #개발자 #study #취준
- 프로그래머스 #문자열 뒤집기 #알고리즘 #Java #자바 #코린이 #개발자 #study #공부 #성장
- 프로그래머스 #조건에 맞는 개발자 찾기 #MySQL #쿼리문 #알고리즘 #코린이 #개발자 #study #취준
- 프로그래머스 #조건별로 분류하여 주문상태 출력하기 #MySQL #쿼리문 #알고리즘 #코린이 #개발자 #study #취준
- 프로그래머스 #중복된 문자 제거 #알고리즘 #Java #자바 #코린이 #개발자 #study #공부 #성장
- 프로그래머스 #부분 문자열 이어 붙여 문자열 만들기 #알고리즘 #Java #자바 #코린이 #개발자 #study #취준
- 프로그래머스 #있었는데요 없었습니다 #DB #MySQL #알고리즘 #쿼리문 #코린이 #개발자 #study #공부 #성장
- 프로그래머스 #조건에 맞는 사용자와 총 거래금액 조회하기 #알고리즘 #Mysql #DB #쿼리문 #코린이 #개발자 #study #공부 #성장
- 프로그래머스 #배열만들기 5 #알고리즘 #study #Java #코린이 #개발자 #성장 #공부
- 파이썬 #input() #sys.stdin.readline() #공부 #study
- 프로그래머스 #등차수열의 특정한 항만 더하기 #알고리즘 #Java #자바 #코린이 #개발자 #study #공부
- 프로그래머스 #소수 만들기 #알고리즘 #Java #자바 #코린이 #개발자 #study #취준
Archives
- Today
- Total
luke
[자바/Java] - 진법 변환 (Integer, BigInteger) (n 진수 <-> 10진수) 본문
[자바/Java] - 진법 변환 (Integer, BigInteger) (n 진수 <-> 10진수)
자바 진법 변환에 대해 정리해보려 한다. 알고리즘 문제를 풀면서 생각보다 많이 나와 정리해 두면 좋을 거 같다.
우선 진법 변환 하는데 Integer를 사용해 많이들 한다. 다만 필자는 BigIntegr를 사용하는 경우는 많이 보지 못했는데, 이번에 두 개를 같이 다뤄보려고 한다.
1. 10진수 → n진수
※ 진법 변환할때 10진수를 제외하고 나머지 진수는 String 타입으로 받아야 한다.
int는 전부 10진수로 인식하고 2진수, 8진수, 16진수는 Binary, Octal, Hex 함수가 존재한다.
(toString() 함수를 사용해도 괜찮다. )
<예시>
public class Main {
public static void main(String[] args) {
int num = 35;
System.out.println("10진수 -> 2진수");
System.out.println(Integer.toBinaryString(num));
System.out.println(Integer.toString(num,2));
System.out.println("10진수 -> 3진수");
System.out.println(Integer.toString(num, 3));
System.out.println("10진수 -> 4진수");
System.out.println(Integer.toString(num, 4));
System.out.println("10진수 -> 5진수");
System.out.println(Integer.toString(num, 5));
System.out.println("10진수 -> 6진수");
System.out.println(Integer.toString(num, 6));
System.out.println("10진수 -> 7진수");
System.out.println(Integer.toString(num, 7));
System.out.println("10진수 -> 8진수");
System.out.println(Integer.toOctalString(num));
System.out.println(Integer.toString(num,8));
System.out.println("10진수 -> 16진수");
System.out.println(Integer.toHexString(num));
System.out.println(Integer.toString(num,16));
}
}
<결과>

<BigInteger 예시>
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
String str = "35";
BigInteger big = new BigInteger(str);
System.out.println("10진수 -> 2진수");
System.out.println(big.toString(2));
System.out.println("10진수 -> 3진수");
System.out.println(big.toString( 3));
System.out.println("10진수 -> 4진수");
System.out.println(big.toString( 4));
System.out.println("10진수 -> 5진수");
System.out.println(big.toString( 5));
System.out.println("10진수 -> 6진수");
System.out.println(big.toString( 6));
System.out.println("10진수 -> 7진수");
System.out.println(big.toString( 7));
System.out.println("10진수 -> 8진수");
System.out.println(big.toString(8));
System.out.println("10진수 -> 16진수");
System.out.println(big.toString(16));
}
}
결과는 위 결과와 똑같이 나온다.
다만 다른점은 int형이 아닌 String 문자열로 받아 BigInteger 형변환을 통해 진수를 변환시켜 준다.
또 Integr.toString(num,2) 이런식으로 했다면 BigInteger는 big.toString(2)이다.
2. n진수 → 10진수
※ 10진수로 변환할 때 String타입으로 받은 값은 Integer.parseInt(String s, int radix) 함수에 넣는데 int값 radix 에는 str값이 몇 진수 값 인지 넣어줘야 한다.
<예시>
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
String str = "100011";
System.out.println("2진수 -> 10진수");
System.out.println(Integer.parseInt(str,2));
System.out.println("3진수 -> 10진수");
System.out.println(Integer.parseInt(str,3));
System.out.println("4진수 -> 10진수");
System.out.println(Integer.parseInt(str,4));
System.out.println("5진수 -> 10진수");
System.out.println(Integer.parseInt(str, 5));
System.out.println("6진수 -> 10진수");
System.out.println(Integer.parseInt(str, 6));
System.out.println("7진수 -> 10진수");
System.out.println(Integer.parseInt(str, 7));
System.out.println("8진수 -> 10진수");
System.out.println(Integer.parseInt(str,8));
System.out.println("16진수 -> 10진수");
System.out.println(Integer.parseInt(str,16));
}
}
<결과>

<BigInteger 예시>
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
String str = "100011";
for (int i = 2; i <= 8; i++) {
System.out.println(i+"진수 -> 10진수");
BigInteger big = new BigInteger(str,i);
System.out.println(big);
}
System.out.println("16진수 -> 10진수");
BigInteger big = new BigInteger(str,16);
System.out.println(big);
}
}
n진수를 10진수로 변환할 때는 BigInteger(문자열(), i진수)로 객체를 생성한 뒤 출력해줘야 한다. 위 코드 또한 결과는 같다.
'Study > Java' 카테고리의 다른 글
| [자바 / Java] - 문자열 변환 ( Integer.toString(), String.valueOf() ) (0) | 2025.11.05 |
|---|---|
| [자바/Java] - Stack(스택) 정리 및 활용 (0) | 2024.04.29 |
| [자바/Java] - indexOf(), substring() (문자열 자르기) (0) | 2024.04.21 |
| [자바/Java] - compareTo() (문자열, 숫자 비교) (0) | 2024.04.17 |
| [자바/Java] - instanceof 연산자 (0) | 2024.01.14 |