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

package com.javatutorialhq.tutorial;

import java.math.BigInteger;

public class BinarytoBigInteger {

	 * This java sample code shows how to convert a string input in binary
	 * format into a BigInteger
	 * Property of
	 * You are free to modify on your own personal use
	 * All Rights Reserved
	 * Version 1.0
	 * 07/24/2013

	public static void main(String[] args) {
		String binaryString = "1010110101011";
		try {
			BigInteger bigNumber = new BigInteger(binaryString, 2);
			System.out.println("BigNumber is: " + bigNumber);
		} catch (NumberFormatException ne) {
			System.out.println("Binary input is invalid");



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