5

I have a binary string:

1010010111100101100010101010011011010001111100000010101000000000010000000111110111100"

How can I convert it to a hex string?

I tried with the wrapper classes Long and Integer, but it didn’t work for me, throwing a NumberFormatException.

3
  • Calculate the decimal value then use Integer.toHexString(value); Commented Jun 4, 2013 at 12:56
  • 4
    possible duplicate of Translating a String containing a binary value to Hex - This was the very first Google result using java Convert Binary String to Hex String Commented Jun 4, 2013 at 12:56
  • 1
    @JohnB The answer there refers to using int / long, which are too small in this instance. Commented Jun 4, 2013 at 13:01

5 Answers 5

11

You need to use BigInteger for this - the number is too big to fit in a primitive value. The biggest number that can be stored in a long is 9223372036854775807, whereas the equivalent value in decimal of this binary string is much bigger, 25069592479040759763832764. That's why you're getting the NumberFormatException.

So with BigInteger:

String s = "1010010111100101100010101010011011010001111100000010101000000000010000000111110111100";
BigInteger b = new BigInteger(s, 2);
System.out.println(b.toString(16));

...which gives:

14bcb154da3e0540080fbc
Sign up to request clarification or add additional context in comments.

1 Comment

WARNING - if you use the same method for binary strings that may begin with zeros, they may be dropped when converting to hex.
2

As the length of your binary String may exceed the capacity of an Integer or Long, it's best to use BigInteger. Remember that in Java an int is always 32 bits, and a long 64 bits.

String binaryString = "1010010111100101100010101010011011010001111100000010101000000000010000000111110111100";
String hexString = new BigInteger(binaryString, 2).toString(16);

Comments

2
public static String convertBinaryToHex(String binInPut) {
    int chunkLength = binInPut.length() / 4, startIndex = 0, endIndex = 4;
    String chunkVal = null;
    for (int i = 0; i < chunkLength; i++) {
        chunkVal = binInPut.substring(startIndex, endIndex);
         System.out.println(Integer.toHexString(Integer.parseInt(chunkVal, 2)));
        startIndex = endIndex;
        endIndex = endIndex + 4;
    }

    return binInPut;

}

Comments

1

If you are using big numbers:

String hexString = new BigInteger(binaryString, 2).toString(16);

1 Comment

BigInteger will trim leading zeros. This can be a problem if the bin string start with "00"
0

I use this

fun String.binToHex(): String {
    val out = StringBuilder()
    val outArray = this.deviceInParts(4)
    outArray.forEach {
        if (it == "0000") out.append('0')
        if (it == "0001") out.append('1')
        if (it == "0010") out.append('2')
        if (it == "0011") out.append('3')
        if (it == "0100") out.append('4')
        if (it == "0101") out.append('5')
        if (it == "0110") out.append('6')
        if (it == "0111") out.append('7')
        if (it == "1000") out.append('8')
        if (it == "1001") out.append('9')
        if (it == "1010") out.append('A')
        if (it == "1011") out.append('B')
        if (it == "1100") out.append('C')
        if (it == "1101") out.append('D')
        if (it == "1110") out.append('E')
        if (it == "1111") out.append('F')
    }
    return out.toString()
}

fun String.deviceInParts(parts: Int): ArrayList<String> {
    val outArray = arrayListOf<String>()
    for (i in 0 until this.length step parts) {
        outArray.add(this.subString(i, parts))
    }
    return outArray
}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.