Top community members
All Wiki Articles Create Wiki Article

Have you ever heard about encyclopedia for code?

What about: Wiki for Code project?

* because I've tried to share code and my solutions on different services and it was removed or I was unwelcomed
   - maybe it wasn't good place, but this one is.

if you think you have solution that can help someone, you are welcome to share this solution - Click here

Java - convert int to binary String with leading zeros

0 contributions
13 points

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

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey đź‘‹
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more