JAVA/JAVA
Java | Integer.to~String()
pathas
2020. 1. 31. 17:05
Integer.to~String()
Integer 클래스 내장 메소드, 인자로 들어온 int형 숫자를 특정 진수의 숫자로 반환
메소드 | 설명 |
---|---|
Integer.toBinaryString(int i) | Integer 클래스 정적 메소드, 인자로 들어온 int형 숫자를 2진수 문자열로 반환 |
Integer.toOctalString(int i) | Integer 클래스 정적 메소드, 인자로 들어온 int형 숫자를 8진수 문자열로 반환 |
Integer.toHexString(int i) | Integer 클래스 정적 메소드, 인자로 들어온 int형 숫자를 16진수 문자열로 반환 |
Integer.toString(int i, int radix) | Integer 클래스 정적 메소드, 인자로 들어온 int형 숫자를 radix(기수) 진수 문자열로 반환 Integer.toString(int i)와 함께 일반 메소드 toString()의 오버로딩 메소드 |
※ 기수(radix, base): 숫자 표현(진법 체계)의 기준이 되는 수
사용례
package j200131;
public class IntStr {
public static void main(String[] args) {
int i = 144;
// Integer.toBinaryString(int i)
String binary = Integer.toBinaryString(i);
// Integer.toOctalString(int i)
String octal = Integer.toOctalString(i);
// Integer.toHexString(int i)
String hex = Integer.toHexString(i);
// Integet.toString(int i, int radix)
String binaryRadix = Integer.toString(i, 2);
String octalRadix = Integer.toString(i, 8);
String hexRadix = Integer.toString(i, 16);
System.out.println("2진수: " + binary); // 2진수: 10010000
System.out.println("8진수: " + octal); // 8진수: 220
System.out.println("16진수: " + hex); // 16진수: 90
System.out.println("2진수: " + binaryRadix); // 2진수: 10010000
System.out.println("8진수: " + octalRadix); // 8진수: 220
System.out.println("16진수: " + hexRadix); // 16진수: 90
}
}
매개변수로 입력된 정수를 다양한 숫자 체계로 표현할 수 있는 Integer.to~String() 메소드에 대해 알아보았습니다.
코드를 작성하다 보면 10진수 이외의 진법 체계로 수를 표현해야 하는 경우가 발생하는데,
그럴 때 Integer.to~String() 메소드를 사용하면 보다 편리하게 수를 다양한 진법 체계로 나타낼 수 있겠습니다.