0
int i = 10000;
Integer.toBinaryString(i);

How can I make the Integer.toBinaryString method return leading zeroes as well? For example, for i = 1000, I want 00000000000000000000001111101000 to appear, not 1111101000.

Sean Mickey
7,7362 gold badges34 silver badges59 bronze badges
asked Mar 8, 2016 at 0:48
5
  • And yes, i did read there's other ways by doing it, but i dont want to copy n paste as i thought this would do the job Commented Mar 8, 2016 at 0:49
  • 1
    From the documentation: This value is converted to a string of ASCII digits in binary (base 2) with no extra leading 0s. docs.oracle.com/javase/7/docs/api/java/lang/… Commented Mar 8, 2016 at 0:51
  • But whats up with the wierd result? What am i doing wrong? Commented Mar 8, 2016 at 0:53
  • The result is not weird it's binary (you know, one's and zero's). If you look at a Decimal to Binary Converter you will see that it is correct just without the leading zero's binaryhexconverter.com/decimal-to-binary-converter. If you just want the int in a String representation of it's int value, use Integer.toString() Commented Mar 8, 2016 at 0:56
  • Okay sorry it's because i was talking about 8 bits only, sorry, and thanks for help! :) Commented Mar 8, 2016 at 1:01

1 Answer 1

1

If you want to left pad the result with zeros, you could do:

String raw = Integer.toBinaryString(i);
String padded = "0000000000000000".substring(raw.length()) + raw;

Here I chose a width of 16 digits, you can adjust the width by the number of zeros in the string.

Note, if it is possible that i > 2^16 - 1 then this will fail and you'll need to protect against that (32 zeros would be one approach).

EDIT

Here's a more complicated version which formats to the smallest of 8, 16, 24, or 32 bits which will contain the result:

public class pad {
 public static String pbi ( int i ) {
 String raw = Integer.toBinaryString(i);
 int n = raw.length();
 String zeros;
 switch ((n-1)/8) {
 case 0: zeros = "00000000"; break;
 case 1: zeros = "0000000000000000"; break;
 case 2: zeros = "000000000000000000000000"; break;
 case 3: zeros = "00000000000000000000000000000000"; break;
 default: return raw;
 }
 return zeros.substring(n) + raw;
 }
 public static void main ( String[] args ) {
 Scanner s = new Scanner(System.in);
 System.out.print("Enter an integer : ");
 int i = s.nextInt();
 System.out.println( pbi( i ) );
 }
}
answered Mar 8, 2016 at 0:56
Sign up to request clarification or add additional context in comments.

2 Comments

Wouldn't this throw a error if its over 16 digits??
Yup, if that's a possibility, you'll need to deal with it somehow.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.