Parse hexadecimal string to create BigInteger in Java

Java 8Object Oriented ProgrammingProgramming

To parse the hexadecimal string to create BigInteger, use the following BigInteger constructor and set the radix as 16 for Hexadecimal.

BigInteger(String val, int radix)

This constructor is used to translate the String representation of a BigInteger in the specified radix into a BigInteger.

In the below example, we have set the BigInteger and radix is set as 16 −


 Live Demo

import java.math.*;
public class Demo {
   public static void main(String[] args) {
      BigInteger one, two;
      String hexStr = "290f98";
      one = new BigInteger("250");
      // parsing
      two = new BigInteger(hexStr, 16);
      System.out.println("Result (BigInteger) : " +one);
      System.out.println("Result (Parsing Hex String) : " +two);


Result (BigInteger) : 250
Result (Parsing Hex String) : 2690968
Updated on 29-Jun-2020 05:46:43