//convert hexadecimal to binary using BigInt
public static String hexToBinary(String hexNumber)
{
//BigInteger temp = new BigInteger(hexNumber, 16);
//return temp.toString(2);
String s = hexNumber;
byte[] bytes = s.getBytes();
StringBuilder binary = new StringBuilder();
for (byte b : bytes)
{
int val = b;
for (int i = 0; i < 8; i++)
{
binary.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
binary.append(' ');
}
System.out.println("'" + s + "' to binary: " + binary);
return binary.toString();
}
String hexNumber is "0957" and the function returns "00110000 00111001 00110101 00110111"...I was expecting "0000 1001 0101 0111". Can someone identify what the problem is? Thanks.