How to show zeros to the left of a binary number in java?

2

I am trying to convert a hexadecimal number into binary and count the positions where the 1 are, but at the time of doing so, it does not show the 0 on the left side, so the position is affected.

For example, I have the hexadecimal number 28C28805 , when I convert it to binary I get this way * 00 * 10 1000 1100 0010 1000 1000 0000 0101 but when I run the program I shows this result 10 1000 1100 0010 1000 1000 0000 0101 where you omit the first two 00.

Is there any way to show those digits that by logic does not show?

Annex the code with which I currently work:

package examenbitmap;
import java.io.*;
import java.lang.*;

public class ExamenBitmap {

    public static void main(String[] args) throws IOException
    {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Ingrese el bit primario: ");
        String hex = bf.readLine();


       System.out.println("Ingrese el bit secundario: ");

       String hexa = bf.readLine();

        int y = Integer.parseInt(hex,16);
        int x = Integer.parseInt(hexa,16);

        String bin = Integer.toBinaryString(y);
        String binx= Integer.toBinaryString(x);

        String biny= Integer.toBinaryString(y) + Integer.toBinaryString(x);

        System.out.println("El valor del bitmap primario es: "  + biny);
        //System.out.println("El valor del bitmap secundario es: " + binx);
        System.out.println("Posicion de cada uno en la cadena: ");

        for(int i=0;i<biny.length();i++){
            if ('1'==biny.charAt(i)) {
                if (i>0){
                    System.out.print(",");
                }
                System.out.print(String.valueOf(i+1));
            }
        }
            System.out.println("");
    }
}
    
asked by Alberto Rodriguez 26.07.2017 в 05:39
source

1 answer

3

To convert from Hexadecimal to Binary, you can better use the class BigInteger , because if you use Integer you will have problems with the values of more than 32 bits. To use it, it is necessary to import the class.

To get a fixed chain of 32 positions, you can do something like this:

String valor32=String.format("%32s", valor).replace(" ", "0");

I'll give you an example with both possibilities.

Example: Ver Demo

import java.math.BigInteger;


class Rextester
{  
    public static void main(String args[])
    {

          String s="28C28805";

          String valorInt=hexToBinaryI(s);
          System.out.println(valorInt);

          //32 bits
          String valorInt32=String.format("%32s", valorInt).replace(" ", "0");
          System.out.println(valorInt32);

          String valorBigInt=hexToBinary(s);
          System.out.println(valorBigInt);

          //32 bits
          String valorBigInt32=String.format("%32s", valorBigInt).replace(" ", "0");
          System.out.println(valorBigInt32);       


    }

    //Usando BigInt
    public static String hexToBinary(String hex) 
    {
        return new BigInteger(hex, 16).toString(2);
    }


    //Usando Int
    public static String hexToBinaryI(String hex) 
    {
        int i = Integer.parseInt(hex, 16);
        String binaryString = Integer.toBinaryString(i);
        return binaryString;
    }

}

Resultado:

101000110000101000100000000101
00101000110000101000100000000101
101000110000101000100000000101
00101000110000101000100000000101
    
answered by 26.07.2017 в 07:41