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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With