This section of java tutorial show how to convert a binary string input to BigInteger format. BigInteger of java.math package is commonly selected due to its characteristics that can hold a large number. Given also a scenario during conversion that the  binary input is not really in binary number, proper handling of exception is shown on the given example source code.

BigInteger has a constructor that accepts a string input and the radix notation or commondly called the base number. In this case it is 2 since we are dealing with binary. So we are done in the conversion. One problem arises when the input is not purely contains 1’s and 0’s. To handle this, it is necessary to sorround our expression with a try catch block and handle the exception gracefully.

Instructions in converting a binary input to BigInteger in java

Given a string input 1110101 which is in binary format, convert it into BigInteger and display in console the converted value. Program must able to determine if the input is in binary format and gives a message ‘Binary Input is invalid’ if otherwise

Java Source Code that reads A binary string input from Console and Convert it into BigInteger

If you have any questions regarding the posted java source code example, please feel free to leave a comments below.