Languages
[Edit]
EN

Java - convert int to binary String with leading zeros

13 points
Created by:
Palusbug
515

Short solution:

int number = 8;

String binaryString = Integer.toBinaryString(number);
String withLeadingZeros = String.format("%8s", binaryString).replace(' ', '0');

System.out.println(binaryString);     // 1000
System.out.println(withLeadingZeros); // 00001000

In java simples way to convert int to binary String with leading zeros is to use Integer.toBinaryString(number) and String.format() method. Note that as first param in String format we pass number of leading spaces and we replace all spaces with 0.

1. Print int and binary representation of a number

public class Example1 {

    public static void main(String[] args) {

        System.out.println("int | binary");

        int number = 8;

        String binary = Integer.toBinaryString(number);
        String padding = String.format("%8s", binary).replace(' ', '0');
        String intPadding = String.format("%3s", number);

        System.out.println(intPadding + " - " + padding);
    }
}

Output:

int | binary
  8 - 00001000

2. Print int and binary representation between 0 and 8

public class Example2 {

    public static void main(String[] args) {

        System.out.println("int | binary");

        for (int i = 0; i <= 8; i++) {

            String binary = Integer.toBinaryString(i);
            String padding = String.format("%8s", binary).replace(' ', '0');
            String intPadding = String.format("%3s", i);

            System.out.println(intPadding + " - " + padding);
        }
    }
}

Output:

int | binary
  0 - 00000000
  1 - 00000001
  2 - 00000010
  3 - 00000011
  4 - 00000100
  5 - 00000101
  6 - 00000110
  7 - 00000111
  8 - 00001000

3. Print int and binary representation between 0 and 255 - ASCII

public class Example3 {

    public static void main(String[] args) {

        for (int i = 0; i <= 255; i++) {

            String binary = Integer.toBinaryString(i);
            String padding = String.format("%8s", binary).replace(' ', '0');
            String intPadding = String.format("%3s", i);

            System.out.println(intPadding + " - " + padding);
        }
    }
}

Output:

  0 - 00000000
  1 - 00000001
  2 - 00000010
  3 - 00000011
  4 - 00000100
  5 - 00000101
  6 - 00000110
  7 - 00000111
  8 - 00001000

...

252 - 11111100
253 - 11111101
254 - 11111110
255 - 11111111

Merged questions

  1. Java - convert integer to binary String representation with leading zeros
  2. How to cast int to binary string with leading zeros in java?
  3. Java - print integer in binary format with leading zeros
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java conversion

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join