Parse hexadecimal string to create BigInteger in Java


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 −

Example

 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);
   }
}

Output

Result (BigInteger) : 250
Result (Parsing Hex String) : 2690968

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 29-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements