1

Ok so i want to convert a Hex string to a Binary String so that i can do some bit swaps and subs etc down the line and i come across what i thought was the answer on here Convert hex string to binary string but this caused me some problems.

String hexToBinary(String hexadecimalString) {
    int i = Integer.parseInt(hexadecimalString, 16);
    String binaryString = Integer.toBinaryString(i);
    return binaryString;
}

However, for example if i passed in the hexadecimal string "03" it would only return a binary string of "11". Or if i were to pass in the hex string "41" it would return a binary string of "1000001". How can I make it so that it will always return a binary string of length 8 bits? All help greatly appreciated in advance :)

Tried your suggestion of padding the binary string but it didn't work, this is what i tried, can you see what I've done wrong?

String hexToBinary(String hexString) {
    int i = Integer.parseInt(hexString, 16);
    String binaryString = Integer.toBinaryString(i);
    String padded = String.format("%8s", binaryString.replace(' ', '0'));
    return padded;
}
3
  • Check this. stackoverflow.com/questions/9246326/… Commented Apr 15, 2015 at 10:48
  • 1
    You just need to left-pad with zeroes until your string is 8 characters long Commented Apr 15, 2015 at 10:49
  • 1
    The replace() must be called on the result of String.format() not on the binrayString. Commented Apr 15, 2015 at 11:09

3 Answers 3

1

Use String.format

Try this:

String.format("%8s", Integer.toBinaryString(i)).replace(' ', '0')
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Iswanto, i tried your suggestion but it still returns the same string with no padding?
@lifeisaloop Then you must have done something wrong. As the example is right. For 41 it returns 00101001.
Yes had a look again there and i had mistyped it. Thanks guys for your help!
1
String hexToBinary(String hexadecimalString) {
int i = Integer.parseInt(hexadecimalString, 16);
String binaryString = Integer.toBinaryString(i);
while (binaryString .length() < 8) {
        binaryString  = "0" + binaryString ;
    }
return binaryString;
}

adding 0 at start of string till length of binaryString is 8

Comments

0

Out of many other solutions this could be one.

StringBuilder sb = new StringBuilder();
String binaryString = Integer.toBinaryString(41);
for (int i = binaryString.length(); i < 8; i++) {
    sb.append('0');
}
sb.append(binaryString);
System.out.println(sb.toString());

or this one

StringBuilder sb = new StringBuilder("00000000");
sb.append(Integer.toBinaryString(41));
System.out.println(sb.substring(sb.length()-8));

or even this one

String s = "00000000" + Integer.toBinaryString(41);
System.out.println(s.substring(s.length()-8));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.