解析和格式化 Java 中的 BigInteger 到二进制
首先,获取两个 BigInteger 对象并设置值。
BigInteger one, two;
one = new BigInteger("99");
two = new BigInteger("978");现在,解析 BigInteger 对象“two”为二进制。
two = new BigInteger("1111010010", 2);
String str = two.toString(2);在上文,我们使用了以下构造函数。在此,对于 BigInteger 构造函数和 toString() 方法,基数都设置为表示二进制的 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP