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

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

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

现在,将BigInteger对象“ two”解析为Binary。

two = new BigInteger("1111010010", 2);
String str = two.toString(2);

上面,我们使用了以下构造函数。在这里,BigInteger构造函数和toString()方法的Binary基数均设置为2。

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 Binary
      two = new BigInteger("1111010010", 2);
      String str = two.toString(2);
      System.out.println("Result (BigInteger) : " +one);
      System.out.println("Result after parsing : " +str);
   }
}

输出结果

Result (BigInteger) : 99
Result after parsing : 1111010010