Convert a string containing a binary value to hexadecimal

I am trying to translate a string containing a binary value (e.g. 000010001010011), the value is Hex. (453)

I try several options, but basically I get the converted value of each individual character. (0 = 30 1 = 31)

I have a function that translates my input into binary through a non-mathematical way, but through a series of if if else if statements. (Values ​​are not calculated because they are not standard.) The binary code is contained in the String variable "binOutput"

I currently have something like this:

String bin = Integer.toHexString(Integer.parseInt(binOutput)); 

But this does not work at all.

+7
source share
2 answers

Try using Integer.parseInt(binOutput, 2) instead of Integer.parseInt(binOutput)

+17
source

Ted Hopp beat me, but here anyway:

 jcomeau@intrepid :/tmp$ cat test.java; java test 000010001010011 public class test { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { System.out.println("The value of " + args[i] + " is " + Integer.toHexString(Integer.parseInt(args[i], 2))); } } } The value of 000010001010011 is 453 
+2
source

All Articles