Convert a String to a BigInteger in Java
In Java, the BigInteger class is a part of the java.math package and is designed to handle arbitrary precision integers. Unlike primitive data types such as int or long, which have fixed size limits, BigInteger can represent integers of any size, limited only by the available memory.
Example of String to BigInteger Conversion
Given a String, and converting it into BigInteger Number.
Example 1: Input String: "12345678901234578901201234567890"
Output BigInteger: 12345678901234578901201234567890Example 2: Hexadecimal String: "1b4a5c6d7e8f"
Output BigInteger: 30006192209551
How to Convert a String to a BigInteger in Java?
There are certain methods to convert String to BigInteger in Java as mentioned below:
- Using BigInteger(String) constructor
- using valueOf() method
1. Convert String into BigInteger Using Constructor
Below is the implementation of the above method:
// Java Program to implement
// Convert String to BigInteger
// Using BigInteger Constructor
import java.math.BigInteger;
// Driver Class
class GFG {
// main function
public static void main (String[] args) {
// String Created
String numberStr = "12345678901234578901201234567890";
// Converted BigInteger
BigInteger bigInt = new BigInteger(numberStr);
System.out.println("String: " + numberStr);
System.out.println("BigInteger: " + bigInt);
}
}
Output
String: 12345678901234578901201234567890 BigInteger: 12345678901234578901201234567890
2. Convert using BigInteger.valueOf() method
Below is the implementation of BigInteger.valueOf() method:
// Java Program to implement
// Convert String to BigInteger
// Using BigInteger Constructor
import java.io.*;
import java.math.BigInteger;
// Driver Class
class GFG {
// main function
public static void main(String[] args)
{
// String Declared
String numberStr = "987654323245129963";
// Converted to BigInteger using valueOf()
BigInteger bigInt = BigInteger.valueOf(Long.parseLong(numberStr));
// Result Displayed
System.out.println("String: " + numberStr);
System.out.println("BigInteger: " + bigInt);
}
}
Output
String: 987654323245129963 BigInteger: 987654323245129963
Non-Decimal Integer Strings to BigInteger in Java
Similar to Decimal Number ( Number with Base 10 ) we can convert other number format like Base-2 (Binary), Base-8 (Octal) , Base-16(Hexadecimal) , etc.
Below is the implementation explaining the method for doing this conversion:
// Java Program to implement Convert
// Non Decimal String to BigInteger
import java.io.*;
import java.math.BigInteger;
// Driver Class
class GFG {
// main function
public static void main(String[] args)
{
// Replace this string with your Hexadecimal bigInteger
String hexString = "1b4a5c6d7e8f";
// Convert Hexadecimal string to BigInteger
BigInteger bigInt
= new BigInteger(hexString, 16);
// Print the result
System.out.println("Hexadecimal String: " + hexString);
System.out.println("Equivalent BigInteger: " + bigInt);
}
}
Output
Hexadecimal String: 1b4a5c6d7e8f Equivalent BigInteger: 30006192209551
Explaination of the above Program:
In the above example we are using BigInteger constructor to convert a string hexString which contain a String Number of Base 16. It explain the fact that if we want to convert string str with base x then we can use the syntax BigInteger res = new BigInteger( str, x );