Convert Hexadecimal To Binary Using Java

Here is a useful method to convert a hexadecimal to binary using Java. If you may wonder why the HEX_String object contains 0 to 9 and A to F, it is because a hexadecimal or base-16 notation uses 16 different digits: 0 up to 9 and then the letters A, B, C, D, E, F to represent 10, 11, 12, 13, 14 and 15.

Related Posts Plugin for WordPress, Blogger...

3 comments

  1. public class HexData
    {
    public static void main(String[] args)
    {
    String hexData = “0023456789ABCDEF”;
    long longData;
    String binData = null;
    int balZero = 0;
    System.out.println(“Hex Data : ” + hexData);
    longData = Long.parseLong(hexData, 16);
    System.out.println(“Long Data : ” + longData);
    binData = Long.toBinaryString(longData);
    balZero = (64 – (binData.length()-1));
    for(int i=1;i<balZero;i++)
    {
    binData = "0" + binData;
    }
    System.out.println("Bin Data : " + binData);
    }
    }

  2. Buddy awesome program just a minute bug

    “balZero = (64 – (binData.length()-1));”

    here 64 will be 4*(length of the hex digits)
    If we dont take the hex digits to be a constant

    String hexData= “123F”;
    int len=hexData.length();
    .
    .
    .
    balZero = ((4*len) – (binData.length()-1));

Leave a Reply

Your email address will not be published. Required fields are marked *