The BigInteger class in Java (in java.math) allows handling arbitrarily large integers. It provides flexible ways to convert to and from primitive types, strings, and byte arrays.

Program: BigInteger Conversion
import java.math.BigInteger;
import java.util.Arrays;
public class BigIntegerConversion {
public static void main(String[] args) {
// Conversion to BigInteger
// From int
int intVal = 1000;
BigInteger fromInt = BigInteger.valueOf(intVal);
// From long
long longVal = 9876543210L;
BigInteger fromLong = BigInteger.valueOf(longVal);
// From decimal string
String decimalString = "12345678901234567890";
BigInteger fromDecimalString = new BigInteger(decimalString);
// From binary string
String binaryString = "1010101010";
BigInteger fromBinaryString = new BigInteger(binaryString, 2);
// From hex string
String hexString = "1F4A9";
BigInteger fromHexString = new BigInteger(hexString, 16);
// From byte array
byte[] byteArray = {0x01, 0x02, 0x03}; // equivalent to 66051
BigInteger fromBytes = new BigInteger(byteArray);
// Conversion from BigInteger
int backToInt = fromInt.intValue();
long backToLong = fromLong.longValue();
double backToDouble = fromDecimalString.doubleValue();
String backToString = fromDecimalString.toString(); // decimal string
String backToBinary = fromBinaryString.toString(2); // binary string
String backToHex = fromHexString.toString(16); // hex string
byte[] backToByteArray = fromHexString.toByteArray();
// Output
System.out.println("---- Conversion TO BigInteger ----");
System.out.println("From int: " + fromInt);
System.out.println("From long: " + fromLong);
System.out.println("From decimal string: " + fromDecimalString);
System.out.println("From binary string: " + fromBinaryString);
System.out.println("From hex string: " + fromHexString);
System.out.println("From byte array: " + fromBytes);
System.out.println("\n---- Conversion FROM BigInteger ----");
System.out.println("To int: " + backToInt);
System.out.println("To long: " + backToLong);
System.out.println("To double: " + backToDouble);
System.out.println("To string (decimal): " + backToString);
System.out.println("To string (binary): " + backToBinary);
System.out.println("To string (hex): " + backToHex);
System.out.println("To byte array: " + Arrays.toString(backToByteArray));
}
}
