用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(Stringval,intradix)
此构造函数用于将指定基数中的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
热门推荐
10 读书祝福语女生短句简短
11 感谢同事新婚祝福语简短
12 感谢教官文案祝福语简短
13 员工聚会蛋糕祝福语简短
14 元宵发客户祝福语简短
15 给团队信心祝福语简短
16 医美的祝福语大全简短
17 异国姻缘的祝福语简短
18 兄妹拍照祝福语简短的话