Java如何将BigInteger转换为另一个基数?
在此示例中,您将看到如何将java.math.BigInteger数字转换为另一个基数,例如二进制,八进制和十六进制。
package org.nhooo.example.math; import java.math.BigInteger; public class BigIntegerConversion { public static void main(String[] args) { BigInteger number = new BigInteger("2017"); System.out.println("Number = " + number); System.out.println("Binary = " + number.toString(2)); System.out.println("Octal = " + number.toString(8)); System.out.println("Hexadecimal = " + number.toString(16)); number = new BigInteger("FF", 16); System.out.println("Number = " + number); System.out.println("Number = " + number.toString(16)); } }
我们的例子的结果:
Number = 2017 Binary = 11111100001 Octal = 3741 Hexadecimal = 7e1 Number = 255 Number = ff