如何在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

更新於: 30-Jul-2019

959次瀏覽

開啟你的 職業生涯

完成課程獲取認證

開始
廣告