Parse and format to hexadecimal in Java


To parse and format to hexadecimal in Java, we have used the BigInteger class. The java.math.BigInteger class provides operations analogues to all of Java's primitive integer operators and for all relevant methods from java.lang.Math.

In the below example, we have taken BigInteger and created an object. With that, some values have been set by us in the arguments as the hexadecimal and the radix i.e. 16 for hexadecimal.

BigInteger bi = new BigInteger("2ef", 16);

Let us see an example.

Example

 Live Demo

import java.math.*;
public class Demo {
   public static void main( String args[] ) {
      BigInteger bi = new BigInteger("2ef", 16);
      String s = bi.toString(16);
      System.out.println(s);
   }
}

Output

2ef

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 26-Jun-2020

204 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements