Parse and format to hexadecimal in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 13-Nov-2018 07:41:53
Advertisements