n진법 to 10진법
// 2진법을 10진법으로 변환
String binary = "1010";
int decimal = Integer.parseInt(binary, 2);
System.out.println(decimal); // Output: 10
// 16진법을 10진법으로 변환
String hex = "1A";
decimal = Integer.parseInt(hex, 16);
System.out.println(decimal); // Output: 26
// 8진법을 10진법으로 변환
String octal = "17";
decimal = Integer.parseInt(octal, 8);
System.out.println(decimal); // Output: 15
10진법 to n진법
// 10진법을 2진법으로 변환
int decimal = 10;
String binary = Integer.toString(decimal, 2);
System.out.println(binary); // Output: 1010
// 10진법을 16진법으로 변환
decimal = 26;
String hex = Integer.toString(decimal, 16);
System.out.println(hex); // Output: 1a
// 10진법을 8진법으로 변환
decimal = 15;
String octal = Integer.toString(decimal, 8);
System.out.println(octal); // Output: 17
'알고리즘 > 도구' 카테고리의 다른 글
[도구, Binary Search] 이분탐색의 세 가지 경우 (0) | 2024.11.09 |
---|---|
[도구, 자료구조] Set과 HashSet vs TreeSet (0) | 2024.10.16 |
[도구, Java] char to int, int to char: ASCII code (0) | 2024.09.15 |
[도구, Java] 다익스트라(Dijkstra) 알고리즘 (0) | 2024.07.08 |
[도구, 자료구조] Priority Queue(우선순위 큐) (0) | 2024.05.20 |