10진수 → n진수
public class Test {
public static void main(String[] args) {
int k = 15;
System.out.println("10진수 → 2진수");
System.out.println(Integer.toString(k,2));
System.out.println(Integer.toBinaryString(k));
System.out.println();
System.out.println("10진수 → 3진수");
System.out.println(Integer.toString(k,3));
System.out.println();
System.out.println("10진수 → 4진수");
System.out.println(Integer.toString(k,4));
System.out.println();
System.out.println("10진수 → 5진수");
System.out.println(Integer.toString(k,5));
System.out.println();
System.out.println("10진수 → 6진수");
System.out.println(Integer.toString(k,6));
System.out.println();
System.out.println("10진수 → 7진수");
System.out.println(Integer.toString(k,7));
System.out.println();
System.out.println("10진수 → 8진수");
System.out.println(Integer.toString(k,8));
System.out.println(Integer.toOctalString(k));
System.out.println();
System.out.println("10진수 → 16진수");
System.out.println(Integer.toString(k,16));
System.out.println(Integer.toHexString(k));
System.out.println();
}
}
n진수 → 10진수
public class Solution {
public static void main(String[] args) {
String k = "1111";
System.out.println("2진수 → 10진수");
System.out.println(Integer.parseInt(k, 2));
System.out.println();
System.out.println("3진수 → 10진수");
System.out.println(Integer.parseInt(k, 3));
System.out.println();
System.out.println("4진수 → 10진수");
System.out.println(Integer.parseInt(k, 4));
System.out.println();
System.out.println("5진수 → 10진수");
System.out.println(Integer.parseInt(k, 5));
System.out.println();
System.out.println("6진수 → 10진수");
System.out.println(Integer.parseInt(k, 6));
System.out.println();
System.out.println("7진수 → 10진수");
System.out.println(Integer.parseInt(k, 7));
System.out.println();
System.out.println("8진수 → 10진수");
System.out.println(Integer.parseInt(k, 8));
System.out.println();
System.out.println("16진수 → 10진수");
System.out.println(Integer.parseInt(k, 16));
System.out.println();
}
}
10진수에서 n진수로 변환할 때는 Integer.toString(변환하려는 수, n진수)
n진수에서 10진수로 변환할 때는 Integer.parseInt(변환하려는 수, n진수)
'Develop > Java' 카테고리의 다른 글
2개의 List를 Stream을 사용하여 비교하기 (0) | 2023.02.17 |
---|---|
Map 순회하기 (0) | 2023.02.10 |
ArrayList vs LinkedList (0) | 2023.02.09 |
BigDecimal (0) | 2023.01.25 |
중첩 삼항연산자 (0) | 2022.11.26 |