解析和格式化 Java 中的位元組陣列為二進位制


設定 BigInteger 物件。

BigInteger one;

現在,建立一個 ByteArray。

byte byteArr[] = new byte[] { 0x1, 0x00, 0x00 };
one = new BigInteger(byteArr);

對於Binary,我們使用 2 作為 toString() 方法引數。

String strResult = one.toString(2);

下面是一個示例 −

示例

 線上示例

import java.math.*;
public class Demo {
   public static void main(String[] args) {
      // BigInteger object
      BigInteger one;
      byte byteArr[] = new byte[] { 0x1, 0x00, 0x00 };
      one = new BigInteger(byteArr);
      String strResult = one.toString(2);
      System.out.println("ByteArray to Binary = "+strResult);
   }
}

輸出

ByteArray to Binary = 10000000000000000

更新於: 2020-06-29

3K+ 閱讀

開啟你的 職業生涯

完成課程以獲得認證

開始吧
廣告
© . All rights reserved.