-
반응형
- n진수를 10진수로, 10진수를 n진수로 변환하는 소스코드이다.
- n진수는 String 타입으로 표현하고 10진수는 int 타입으로 표현한다.
- n의 범위는 2 <= n <= 36 이다.
- 라이브러리를 사용하는 경우 손쉽게 10진법 <-> 2, 8, 16진법 간 변환이 가능하지만 그 외의 진법에 대해서는 사용자 정의 메서드를 사용해야한다.
import java.util.ArrayList; public class BaseConversion { public static void main(String[] args) { System.out.println("============ 10 to n ============"); int intNum = 255; //1. 라이브러리를 사용하여 10진수를 2, 8, 16진수로 변환 System.out.println("Binary String with library: " + Integer.toBinaryString(intNum)); System.out.println("Octal String with library: " + Integer.toOctalString(intNum)); System.out.println("Hex String with library: " + Integer.toHexString(intNum)); //2. 사용자 정의 메서드를 사용하여 변환. String binaryNum = baseConvert(intNum, 2); String octNum = baseConvert(intNum, 8); String hexNum = baseConvert(intNum, 16); System.out.println("Binary String with my method: " + binaryNum); System.out.println("Octal String with my method: " + octNum); System.out.println("Hex String with my method: " + hexNum); System.out.println("\n============ n to 10 ============"); String hexStr = "FFFFFFF"; //1. 라이브러리를 사용하여 n진수를 10진수로 변환 System.out.println("16 -> 10 (with library): " + Integer.parseInt(hexStr, 16)); //2. 사용자 정의 메서드를 사용하여 n진수를 10진수로 변환 System.out.println("16 -> 10 (no library): " + convertToDecimal(hexStr, 16)); } public static int convertToDecimal(String strNum, int base) { int result = convertToInt(strNum.charAt(strNum.length() - 1)); int multiplier = base; for(int i = strNum.length() - 2; i >= 0; i--) { int num = convertToInt(strNum.charAt(i)); result += num * multiplier; multiplier *= base; } return result; } public static int convertToInt(char c) { switch(c) { case 'A': case 'a': return 10; case 'B': case 'b': return 11; case 'C': case 'c': return 12; case 'D': case 'd': return 13; case 'E': case 'e': return 14; case 'F': case 'f': return 15; case 'G': case 'g': return 16; case 'H': case 'h': return 17; case 'I': case 'i': return 18; case 'J': case 'j': return 19; case 'K': case 'k': return 20; case 'L': case 'l': return 21; case 'M': case 'm': return 22; case 'N': case 'n': return 23; case 'O': case 'o': return 24; case 'P': case 'p': return 25; case 'Q': case 'q': return 26; case 'R': case 'r': return 27; case 'S': case 's': return 28; case 'T': case 't': return 29; case 'U': case 'u': return 30; case 'V': case 'v': return 31; case 'W': case 'w': return 32; case 'X': case 'x': return 33; case 'Y': case 'y': return 34; case 'Z': case 'z': return 35; default: return c - '0'; } } public static String baseConvert(int num, int base) { ArrayList<String> list = new ArrayList<>(); if(base < 10) { while(num > 0) { list.add(Integer.toString(num % base)); num /= base; } } else if(base <= 36) { while(num > 0) { String strNum; switch(num % base) { case 10: strNum = "a"; break; case 11: strNum = "b"; break; case 12: strNum = "c"; break; case 13: strNum = "d"; break; case 14: strNum = "e"; break; case 15: strNum = "f"; break; case 16: strNum = "g"; break; case 17: strNum = "h"; break; case 18: strNum = "i"; break; case 19: strNum = "j"; break; case 20: strNum = "k"; break; case 21: strNum = "l"; break; case 22: strNum = "m"; break; case 23: strNum = "n"; break; case 24: strNum = "o"; break; case 25: strNum = "p"; break; case 26: strNum = "q"; break; case 27: strNum = "r"; break; case 28: strNum = "s"; break; case 29: strNum = "t"; break; case 30: strNum = "u"; break; case 31: strNum = "v"; break; case 32: strNum = "w"; break; case 33: strNum = "x"; break; case 34: strNum = "y"; break; case 35: strNum = "z"; break; default: strNum = Integer.toString(num % base); } list.add(strNum); num /= base; } } else { return null; } StringBuilder strResult = new StringBuilder(); for(int i = list.size()-1; i >= 0; i--) { strResult.append(list.get(i)); } return strResult.toString(); } }
'Java' 카테고리의 다른 글
문자열 뒤집기 예제 (0) 2020.12.10 형변환 (type casting) (0) 2020.12.04 라이브러리를 사용한 배열 정렬 (0) 2020.11.30 Map의 정렬 (0) 2020.11.28 Collection Framework (0) 2020.11.24