I have an assignment where I have to convert a hexadecimal to a 16 bit binary string and then compare two of these using bitwise operators. I have a for loop which executes a.charAt[i]
& b.charAt[i]
to a string builder string. Now, I expect that to output a binary number but I've gotten to a point where every time that line executes, it gives me numbers that are not 0 or 1. And it gives me 2 numbers (2 and 3). What am I doing wrong?
Some code:
int bin = 0;
hex = hex.replaceFirst("0x", "");
bin = Integer.parseInt(hex, 16);
hex = String.format("%16s", Integer.toBinaryString(bin));
return hex;
The two hexadecimals I am trying to evaluate are FFF7
and 0001
. I've successfully converted them to binary strings. Also I don't know why but the preceding zeros are not showing up, just the spaces :/
I've looked online extensively for hours and can't seem to find the problem I am having.