Reputation: 93
I want to cast a string (binary digits) to an Integer
like this:
Integer.parseInt("011000010110")
I always get an NumberFormatException
. Is the number of digits too high?
Upvotes: 0
Views: 98
Reputation: 85
All of Java's number classes are base 10. However, I found these two options here:
working with binary numbers in java
The BitSet class, or a way to declare an int as a binary number (Java 7+). The latter may not work for you depending on how you're obtaining these numbers.
Upvotes: 0
Reputation: 178263
Yes, the string "011000010110"
is about 11 billion, which is higher than the maximum representable int
, Integer.MAX_VALUE
, 2,147,483,647. Try
Long.parseLong("011000010110")
Or, if you meant it as binary, pass a radix of 2 to parseInt
:
Integer.parseInt("011000010110", 2)
Upvotes: 8