정수를 이진으로 변환하는 방법 :
1 메소드 1 : BigInteger 클래스 사용 :
@test public void test1 () {biginteger b = new biginteger ( "10"); // 1010 system.out.println (b.tostring (2)); // 0 b = new biginteger ( "1"); System.out.println (B.ToString (2)); // 1 B = New BigInteger ( "255"); System.out.println (B.ToString (2)); // 11111111 B = New BigInteger ( "254"); System.out.println (B.ToString (2)); // 11111110}2 메소드 2 : Integer.tobinarystring () 사용
@test public void test () {String str2 = integer.tobinarystring (0); System.out.println (str2); // 0 str2 = integer.tobinarystring (1); System.out.println (str2); // 1 str2 = integer.tobinarystring (255); System.out.println (str2); // 111111111} 위에서 언급 한 바와 같이, 정수를 이진으로 변환하는 것은 실제로 가능하지만, 단점은 숫자가 8 비트 미만인 경우 0은 자동으로 추가되지 않는다는 것입니다.
따라서 8 비트 이진 번호를 얻으면 판단을 추가해야합니다.
@Test public void test3 () {String tempst = ""; 문자열 str2 = integer.tobinarystring (10); // 판사 : 바이너리로 변환 된 경우 8 비트 또는 8 비트 이하로 변환되면 int int bit = 8-str2.length () 후 0을 추가해야합니다. if (str2.length () <8) {for (int j = 0; j <bit; j ++) {str2 = "0"+str2; }} tempstr += str2; System.out.println (tempst); }요약
위의 모든 것은 정수를 이진 코드로 변환하는 Java의 예제에 관한 것입니다. 모든 사람에게 도움이되기를 바랍니다. 관심있는 친구는이 사이트의 다른 관련 주제를 계속 참조 할 수 있습니다. 단점이 있으면 메시지를 남겨 두십시오. 이 사이트를 지원해 주신 친구들에게 감사드립니다!