dwwilson66 - 4 months ago 21x

Java Question

I have an array of Strings that represent Binary numbers (without leading zeroes) that I want to convert to their corresponding base 10 numbers. Consider:

`binary 1011 becomes integer 11`

binary 1001 becomes integer 9

binary 11 becomes integer 3 etc.

What's the best way to proceed? I've been exploring java.lang.number.* without finding a direct conversion method.

`Integer.parseInt(b)`

`toBinaryString`

Answer

You need to specify the radix. There's an overload of `Integer#parseInt()`

which allows you to.

```
int foo = Integer.parseInt("1001", 2);
```

Source (Stackoverflow)

Comments