如何在Java中将BigInteger转换为另一个基数
首先,创建一个BigInteger。
BigInteger val = new BigInteger("198");让我们把它转换为二进制,基数为2。
val.toString(2);
使用基数8将其转换为八进制。
val.toString(8);
使用基数16将其转换为十六进制。
val.toString(16);
以下是一个示例 −
示例
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
BigInteger val = new BigInteger("198");
System.out.println("Value: " + val);
// binary
System.out.println("Converted to Binary: " + val.toString(2));
// octal
System.out.println("Converted to Octal: " + val.toString(8));
// hexadecimal
System.out.println("Converted to Hexadecimal: " + val.toString(16));
}
}输出
Value: 198 Converted to Binary: 11000110 Converted to Octal: 306 Converted to Hexadecimal: c6
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP