You need to specify the radix. There's an overload of Integer#parseInt() which allows you to.
int foo = Integer.parseInt("1001", 2);
Answer from Matt Ball on Stack OverflowVideos
You need to specify the radix. There's an overload of Integer#parseInt() which allows you to.
int foo = Integer.parseInt("1001", 2);
This might work:
public int binaryToInteger(String binary) {
char[] numbers = binary.toCharArray();
int result = 0;
for(int i=numbers.length - 1; i>=0; i--)
if(numbers[i]=='1')
result += Math.pow(2, (numbers.length-i - 1));
return result;
}
Hi, Im trying to code a program that switches bewteen int, string, binary , etc.. I know how to convert string to int and viceversa but how can you convert a binary number like "110" to binary?? Also, is it recommendable to convert it directly from string to int decimal or first convert the string to int binary and then this int to int decimal??
atoi doesn't handle binary numbers, it just interprets them as big decimal numbers. Your problem is that it's too high and you get an integer overflow due to it being interpreted as decimal number.
The solution would be to use stoi, stol or stoll that got added to string in C++11. Call them like
int i = std::stoi("01000101", nullptr, 2);
- The returned value is the converted
intvalue. - The first argument is the
std::stringyou want to convert. - The second is a
size_t *where it'll save the index of the first non digit character. - The third is an
intthat corresponds to the base that'll be used for conversion..
For information on the functions look at its cppreference page.
Note that there are also pre C++11 functions with nearly the same name, as example: strtol compared to the C++11 stol.
They do work for different bases too, but they don't do the error handling in the same way (they especially lack when no conversion could be done on the given string at all e.g trying to convert "hello" to a string) and you should probably prefer the C++11 versions.
To make my point, passing "Hello" to both strtol and the C++11 stol would lead to:
strtolreturns0and doesn't give you any way to identify it as error,stolfrom C++11 throwsstd::invalid_argumentand indicates that something is wrong.
Falsely interpreting something like "Hello" as integers might lead to bugs and should be avoided in my opinion.
But for completeness sake a link to its cppreference page too.
It sounds like you should be using strtol() with 2 as the last argument.
As explained above, Integer.toBinaryString() converts ~0 and ~1 to unsigned int so they will exceed Integer.MAX_VALUE.
You could use long to parse and convert back to int as below.
int base = 2;
for (Integer num : new Integer[] {~0, ~1}) {
String binaryString = Integer.toBinaryString(num);
Long decimal = Long.parseLong(binaryString, base);
System.out.println("INPUT=" + binaryString + " decimal=" + decimal.intValue()) ;
}
From http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html#toBinaryString(int) : the toBinaryString() method converts its input into the binary representation of the "unsigned integer value is the argument plus 232 if the argument is negative".
From http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html#parseInt(java.lang.String,%20int) : the parseInt() method throws NumberFormatException if "The value represented by the string is not a value of type int".
Note that both ~0 and ~1 are negative (-1 and -2 respectively), so will be converted to the binary representations of 232-1 and 232-2 respectively, neither of which can be represented in a value of type int, so causing the NumberFormatException that you are seeing.