Parsing and Formatting a Big Integer into Octal in Java


Firstly, take two BigInteger objects and set values.

BigInteger one, two;
one = new BigInteger("99");

Now, parse BigInteger object “two” into Octal.

two = new BigInteger("1100", 8);
String str = two.toString(8);

Above, we have used the following constructor. Here, radix is set as 8 for Octal. for both BigInteger constructor and toString() method.

BigInteger(String val, int radix)

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

The following is an example −

Example

 Live Demo

import java.math.*;
public class Demo {
   public static void main(String[] args) {
      BigInteger one, two;
      one = new BigInteger("99");
      // parsing BigInteger object "two" into Octal
      two = new BigInteger("1100", 8);
      String str = two.toString(8);
      System.out.println("Result (BigInteger) : " +one);
      System.out.println("Result after parsing : " +str);
   }
}

Output

Result (BigInteger) : 99
Result after parsing : 1100

Updated on: 29-Jun-2020

172 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements