Convert Binary String to Int in Java

Convert a Binary String to Int in Java Using
Integer.parseInt()

Convert a Binary String to Int in Java Using
Math.pow()
Binary is made up of two numbers, 0 and 1, and these numbers are used to write different types of instructions for machines. But it is tough for humans to read these binary codes. This is why there are various techniques to convert the binary into a humanreadable format.
In this tutorial, we will go through the two methods that we can use to convert a binary string to an int
. Our goal is to take the binary and parse it to output an int
that represents that binary number.
Convert a Binary String to Int in Java Using Integer.parseInt()
The first method is Integer.parseInt()
that parses the given string into an int
. When we pass a string and a radix or the base value to Integer.parseInt()
, it returns an int
value that is calculated according to the radix number.
In the example, binaryString
has a binary value that needs to be converted into an int
. Integer.parseInt(binaryString, 2)
does the job for us. The first argument is the string, and the second argument is 2 because a binary is a base2 number system.
If binaryString
contains a nonbinary value, then a NumberFormatException
will be thrown and show the error message.
public class BinaryStringToInt {
public static void main(String[] args) {
try {
String binaryString = "10010";
int foo = Integer.parseInt(binaryString, 2);
System.out.println(foo);
} catch (NumberFormatException e) {
System.out.println("Error: The binary string is not valid");
}
}
}
Output:
18
Convert a Binary String to Int in Java Using Math.pow()
In this method, we will check every character of binaryString
as we know that a string in Java is a sequence of characters. We will need to loop through every character until the length of the string.
The next step is to check the occurrence of 1s in binaryString
as only the 1s are added when we convert a binary to a decimal. If there is a 1, it will first decrease the length of binaryString
with 1 and with the iteration’s value. So, in the case of 101000
, the first character is a one, which means that int len
will hold the value 5 because binaryString.length()
is 6 and the iteration variable i
holds 0, so it means that 6  1  0 will be 5.
Now, as we get 5, and it is passed to Math.pow(base, len)
as the second argument, while the first argument will be the base. It will apply base2 to the numbers and then add all the numbers giving us the result in int
.
public class Main {
public static void main(String[] args) {
String binaryString = "101000";
double convertedDouble = 0;
for (int i = 0; i < binaryString.length(); i++) {
if (binaryString.charAt(i) == '1') {
int len = binaryString.length()  1  i;
convertedDouble += Math.pow(2, len);
}
}
int convertedInt = (int) convertedDouble;
System.out.println(convertedInt);
}
}
Output:
40
Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
LinkedInRelated Article  Java String
 Perform String to String Array Conversion in Java
 Remove Substring From String in Java
 Convert Byte Array in Hex String in Java
 Convert Java String Into Byte
 Generate Random String in Java
 The Swap Method in Java
Related Article  Java Int
 Convert Int to Char in Java
 Convert Int to Double in Java
 Convert Object to Int in Java
 List of Ints in Java
 Convert Integer to Int in Java
 Check if Int Is Null in Java