How to Convert BigInteger to String or String to BigInteger in java with examples?

Learn to Convert BigInteger from/to String with examples.

You can also check my previous posts on the BigInteger class in java.

BigInteger Object in Java

BigInteger is commonly used for storing the numerical big values by the result of arbitrary arithmetic calculations.

It is defined in the java.math package.

The value stored in BigInteger is higher than all primitive data type values. No Upper limit is the maximum value that can store in a big integer.

Conversion of BigInteger to String is one of the days to day tasks for java programmers. Both are used to store different values and there is no way to convert automatically in Java.

A few days back I need to convert the BigInteger to Integer in my programming. It is a simple task to do the same.
In My Previous Post, we discussed the conversion of BigInteger to Integer or vice versa. we can also do the same for conversion to String

We are going to discuss below things

  • How to convert BigInteger to String
  • How to convert String to BigInteger

How to Covert BigInteger to a String object in Java:-

Their multiple ways we can do the conversion One way,to use the BigInteger.toString() method, another way is to use String.valueOf() in java.

Using toString() method

Each class in java has the toString method.

It is a simple thing to do with this method. Let’s create a BigInteger object.

BigInteger class has a constructor and accepts String as a parameter, It creates an object.

BigInteger bi=new BigInteger("123");

As you know every java class has a toString method, To return the String object with for the BigInteger class.

Following is the line of code for returning the String object

String str=bi.toString();

Here is a complete example of a successful use case where string contains a numeric value

import java.math.BigInteger;

public class Test {
    public static void main(String[] args) {

        //The following case works without throwing exception
        BigInteger bigIntegerdemo = new BigInteger("123");
        System.out.println(bigIntegerdemo.toString());
    }
}

Output:

123

Here is an error example where string contains a non-numeric value

import java.math.BigInteger;

public class Test {
    public static void main(String[] args) {
        //The following case doest not works and throws NumberFormatException
        BigInteger bigIntegerdemo1 = new BigInteger("abc");
        System.out.println(bigIntegerdemo1.toString());

    }
}

Output throws an error Exception in thread “main” java.lang.NumberFormatException: For input string: “abc”

Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
  at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
  at java.lang.Integer.parseInt(Integer.java:580)
  at java.math.BigInteger.<init>(BigInteger.java:470)
  at java.math.BigInteger.<init>(BigInteger.java:606)
  at Test.main(Test.java:14)

The disadvantages : In this approach are always needed to pass the numeric value in the form of String for the BigInteger constructor.

If a String like “abc” is passed, it throws NumberFormatException.

using toByteArray() method

Another way,

  • First create a BigInteger object
  • Convert to byte array using toByteArray() method
  • Finally, passing array bytes to String’s byte Constructor
BigInteger bigIntegerdemo2 = new BigInteger("123");
  byte b[]=bigIntegerdemo2.toByteArray();
  String s=new String(b);

How to Convert String to a BigInteger object in Java

We can use BigInteger.toByteArray() method to convert string to BigInteger object

  • First, create a string object using the constructor
  • Create a Biginteger by passing an array of bytes String.getBytes()
  • Hence, Converted into BigInteger object
  • if you want, You can convert to String using the toByteArray method
String msg = new String ("Hi test");
  BigInteger bi = new BigInteger(msg.getBytes());
  System.out.println(new String(bi.toByteArray())); // prints "Hi test"

Conclusion

You learned How to convert a String to BigInteger and BigInteger using multiple approaches.