Is there any method in Java that will convert hexadecimal characters to binary characters?

Tim Rohaly

You can use Integer.parseInt() or Long.parseLong() to convert a hexadecimal representation to a 4-byte int or an 8-byte long, respectively. For example, the Java class file magic number 0xCAFEBABE can be converted to the long value 3405691582 using

long magic = Long.parseLong("CAFEBABE", 16);
where the second argument to the method represents the radix used to encode the first parameter (in this case, base 16, or hexadecimal).

You can also convert hexadecimal number directly into Integer or Long objects using the methods Integer.decode() or Long.decode(), respectively.

If you wish to subsequently print out these values as a String in base 2 (binary), you can use either Integer.toBinaryString() or Long.toBinaryString().

See the API documentation for Integer and Long for more detail.