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

Learn the conversion of BigInteger to and from String with practical examples.

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

BigInteger Object in Java

The BigInteger class is commonly used for storing numerical values resulting from arbitrary arithmetic calculations. It is defined in the java.math package.

The range of values stored in a BigInteger exceeds that of all primitive data types. There is no upper limit to the maximum value that can be stored in a BigInteger.

The conversion of BigInteger to String is a common task for Java programmers. Both types are used to store different values, and there is no automatic conversion in Java.

Some time ago, I needed to convert a BigInteger to an Integer in my programming. It’s a straightforward task, and in a previous post, we discussed the conversion of BigInteger to Integer or vice versa. We can also perform a similar conversion to String.

We are going to discuss below things

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

How to Convert BigInteger to a String object in Java:

There are multiple ways to perform this conversion. One way is to use the BigInteger.toString() method, and another way is to use String.valueOf() in Java.

Using toString() method

Each class in Java has the toString method, making it a simple task to use.

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, which returns the String representation for the BigInteger class.

Here is the code for returning the String object:

String str=bi.toString();

Here is a complete example where the 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 example where the 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)
  • Disadvantages In this approach, you always need to pass the numeric value in the form of a String for the BigInteger constructor. If a String like abc is passed, it throws a NumberFormatException.

  • Using toByteArray() method

    Another way is to create a BigInteger object, convert it to a byte array using the toByteArray() method, and then pass the byte array to the 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 the BigInteger.toByteArray() method to convert a String to a BigInteger object.

  • First, create a String object using the constructor.
  • Create a BigInteger by passing an array of bytes (String.getBytes()).
  • Consequently, convert it into a BigInteger object.
  • If desired, you can convert it back to a 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 have learned how to convert a String to BigInteger and vice versa using multiple approaches.