2

I am trying to convert hexadecimal to Binary but the problem is the result is ignoring the zeroes which I should get on the left hand side which are crucial for me.

My code:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner scan;
    int num;

    System.out.println("HexaDecimal to Binary");
    scan = new Scanner(System.in);

    System.out.println("\nEnter the number :");
    num = Integer.parseInt(scan.nextLine(), 16);

    String binary = Integer.toBinaryString(num);

    System.out.println("Binary Value is : " + binary);

}

OUTPUT : When I am giving the input as 0000000000001a000d00 I should get the output as

00000000000000000000000000000000000000000000000000011010000000000000110100000000

But Instead I get 11010000000000000110100000000 leaving the initial zeroes.

How should I get the exact number. Thanks in advance.

  • The number you get *is* the exact number; leading zeroes have no effect in a positional number system. – nanofarad Apr 09 '15 at 19:53
  • @hexafraction I need the leading zeroes as I have to do the xor operation –  Apr 09 '15 at 19:55
  • 1
    Xor doesn't require the numbers are in a string. You can just use var1^var2, where the two variables are numeric types. You may need to use a big integer in the case of very large numbers, however. – nanofarad Apr 09 '15 at 19:57
  • @hexafraction I am using xor cipher in which I need to get the xor by taking each single character in the binary value. How can I get the leading zeroes for that because without it my answer will be different. –  Apr 09 '15 at 20:01
  • You may find this link here useful: http://stackoverflow.com/questions/4421400/how-to-get-0-padded-binary-representation-of-an-integer-in-java – zxgear Apr 09 '15 at 20:23
  • @JohnH in the link the person knows the number of zeroes he must get. Here I don't know the input so I cannot add as many zeroes, what I am doing is for each initial zero I am adding 4 zeroes but again if the first digit is 1 the binary format is 0001 and therefore I miss 3 zeroes –  Apr 09 '15 at 20:29
  • Just for clarification, you mean that if your input contains 3 leading zeros, then your output must contain 3*5=15 leading zeros? – zxgear Apr 09 '15 at 20:34

1 Answers1

0

You can try solution from link (How to get 0-padded binary representation of an integer in java?) provided by @JohnH, combined with calculating length of binary representation of your hex number. Each hexadecimal digit requires 4 binary digits to represent:

public static void main( String[] args ) {
    Scanner scan;
    int num;

    System.out.println("HexaDecimal to Binary");
    scan = new Scanner(System.in);

    System.out.println("\nEnter the number :");
    String input = scan.nextLine().trim();
    num = Integer.parseInt(input, 16);

    int paddedLength = input.length() * 4;
    String binary = String.format("%"+ paddedLength +"s", Integer.toBinaryString(num)).replace(' ', '0');
    System.out.println("Binary Value is : " + binary);
}

It's not perfect, but should do the trick.

Community
  • 1
  • 1
Maciej Lach
  • 1,479
  • 3
  • 18
  • 25