用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 祝福语驱散霉运的话简短