10진수를 2진수,8진수,16진수로 변환 할 때, Integer 클래스의 toBinaryString, toOctalString, toHexString 함수를 사용 int i = 127; String binaryString = Integer.toBinaryString(i); //2진수 String octalString = Integer.toOctalString(i); //8진수 String hexString = Integer.toHexString(i); //16진수 System.out.println(binaryString); //1111111 System.out.println(octalString); //177 System.out.println(hexString); //7f 반대로 2진수,8진수,16진수를 10진수로 변환하기 위해선 Integer 클래스의 parseInt를 사용하여 쉽게 변환이 가능 int binaryToDecimal = Integer.parseInt(binaryString, 2); int binaryToOctal = Integer.parseInt(octalString, 8); int binaryToHex = Integer.parseInt(hexString, 16); System.out.println(binaryToDecimal); //127 System.out.println(binaryToOctal); //127 System.out.println(binaryToHex); //127 자바 2진수, 8진수, 16진수 변환 시에는 Integer 클래스 API를 활용하면 편하다. (참고 : https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html) 10진수 -> 2진수, 8진수, 16진수 변환
결과
2진수, 8진수, 16진수 -> 10진수로 변환
결과 어제 오늘 내일IT/Java [Java] 10진수 <-> 2진수, 8진수, 16진수로 변환하기hi.anna 2021. 4. 24. 22:11
10진수 -> 2진수, 8진수, 16진수로 변환하기java.lang.Integer의 toBinaryString(), toOctalString(), toHexaString() 메소드를 이용하여 10진수를 2진수, 8진수, 16진수 문자열로 변환할 수 있습니다.
예제
결과
위 코드는 10진수 10을 2진수, 8진수, 16진수로 변환하고 있습니다. 2진수, 8진수, 16진수 -> 10진수java.lang.Integer 클래스의 parseInt() 메소드를 이용하여 2진수, 8진수, 16진수 문자열을 10진수 integer로 변경할 수 있습니다.
예제
결과
위 코드는 2진수, 8진수, 16진수를 10진수로 변환하고 있습니다. |