用Java将大整数解析并将其格式化为八进制

首先,获取两个BigInteger对象并设置值。

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

现在,将BigInteger对象“两个”解析为Octal。

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

上面,我们使用了以下构造函数。在此,八进制的基数设置为8。适用于BigInteger构造函数和toString()方法。

BigInteger(String val,int radix)

此构造函数用于将指定基数中的BigInteger的String表示形式转换为BigInteger。

以下是一个例子-

示例

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

输出结果

Result (BigInteger) : 99
Result after parsing : 1100