Convert BigInteger into Another Radix Number in Java



First, create a BigInteger.

BigInteger val = new BigInteger("198");

Let us convert it to Binary, with radix as 2.

val.toString(2);

Convert it to Octal, with radix as 8.

val.toString(8);

Convert it to HexaDecimal, with radix as 16.

val.toString(16);

The following is an example −

Example

 Live Demo

import java.math.BigInteger;
public class Main {
   public static void main(String[] args) {
      BigInteger val = new BigInteger("198");
      System.out.println("Value: " + val);
      // binary
      System.out.println("Converted to Binary: " + val.toString(2));
      // octal
      System.out.println("Converted to Octal: " + val.toString(8));
      // hexadecimal
      System.out.println("Converted to Hexadecimal: " + val.toString(16));
   }
}

Output

Value: 198
Converted to Binary: 11000110
Converted to Octal: 306
Converted to Hexadecimal: c6
Updated on: 2019-07-30T22:30:24+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements