0

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.

qwerty
  • 2,392
  • 3
  • 30
  • 55
Prince Kishore
  • 5
  • 1
  • 2
  • 4

2 Answers2

1

I think you should look at Integer.parseInt() to convert your hexadecimal strings into integers. Once you've got a numeric representation then your binary operations will be much easier.

If you really require a string binary representation, check out Integer.toBinaryString()

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • I suspect he need to add then to a StringBuilder with something like `sb.append((char) Integer.parseInt(n, 16)));` – Peter Lawrey Oct 04 '12 at 10:48
  • @Brian I did use the Integer.parseInt() to convert the hex into integers but then i used the Integer.toBinaryString() to get the binary code of them. Now I can't think of a way to perform the biwise & operation on the two strings other than the way I have it which clearly isn't working. – Prince Kishore Oct 04 '12 at 11:00
1

I'm assuming that you want to bitwise AND two 16-bit, big-endian, hexadecimal numbers together and display the result as binary.

As others have said using numbers rather than strings would make your life easier - unless this is a constraint of the assignment?

If I were doing this task, with my assumptions and as I understand it, I would:

  • Convert the strings into numbers
  • For each bit of the numbers, from 0x8000 down to and including 0x0001:
    • & the corresponding bits
    • Append the result to a StringBuilder
  • Return the contents of the StringBuilder

To step through corresponding bits of each number I would use bit masking and bit shifting.

N.B. 0x8000 is the top bit of a 16-bit number.

An alternative is to let Java do the lot, however this may not be the point of your assignment:

final int a = Integer.parseInt("FFF7", 16); // 0b1111111111110111
final int b = Integer.parseInt("0001", 16); // 0b0000000000000001

final int result = a & b;

final String output = String.format("%16s", 
    Integer.toBinaryString(result)).replace(' ', '0');

System.out.println(output); 

This will print:

0000000000000001

I hope this helps in some way and that I'm not too far off the mark with my assumptions. Good luck!

Community
  • 1
  • 1
Jonathan
  • 20,053
  • 6
  • 63
  • 70