Languages
[Edit]
PT

Java - converter int em binário String com zeros à esquerda

3 points
Created by:
Alyona
1170

Solução rápida:

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

Em java, a maneira simples de converter int em binário String com zeros à esquerda é usar o método Integer.toBinaryString(number) e String.format()

Observe que, como primeiro parâmetro no formato String, passamos o número de espaços à esquerda e substituímos todos os espaços por 0.  

1.Imprimir representação int e binária de um número

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);
    }
}

Resultado:

int | binary
  8 - 00001000

2. Imprimir representação int e binária entre 0 e 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);
        }
    }
}

Resultado:

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

3. Imprimir representação int e binária entre 0 e 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);
        }
    }
}

Resultado:

  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

Perguntas mescladas

  1. Java - converte número inteiro em representação de String binária com zeros à esquerda
  2. Como converter int para string binária com zeros à esquerda em java?
  3. Java - imprima integer em formato binário com zeros à esquerda
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.
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