[JavaStudy] 7. 자바 진수변환 (10진수 → 2, 8, 16진수)

7525 단어 JavaStudyJavaStudy

10진수를 2, 8, 16진수로 변환

  1. Integer.toBinaryString(int i)

  2. Integer.toOctalString(int i)

  3. Integer.toHexString(int i)

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

Integer.parseInt() = 2, 8, 16진수를 10진수로 변환

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
 
 
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

출처 : https://dpdpwl.tistory.com/92

좋은 웹페이지 즐겨찾기