Development

자바 10진수를 2진수로 변경 (자리수 맞추기)

OOOooOOoo·2018년 5월 1일·조회 6,399

1. 소개

  • 자바에서 10진수를 2진수로 변경하는 방법이다.
  • 자리수를 지정하여 0을 패딩할 수 있다.

2. 코드

StringBuffer deciFormat = new StringBuffer();
for ( int inx=0; inx<n; inx++ ) {
  deciFormat.append("0");
}

System.out.println(i);
String s1 = Integer.toBinaryString(i);
System.out.println(s1);
DecimalFormat df = new DecimalFormat(deciFormat.toString());
String s2 = df.format(Integer.parseInt(s1));
System.out.println(s2);

3. 결과

9
1001
01001

댓글 1

로그인 후 댓글을 남길 수 있습니다.

  • 모나미153모나미153· 2018년 5월 1일
    [code]StringBuffer deciFormat = new StringBuffer(); for ( int inx=0; inx