Parsing and Formatting a Byte Array into Binary in Java


Set a BigInteger object.

BigInteger one;

Now, create a ByteArray.

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

For Binary, we have used 2 as the toString() method parameter.

String strResult = one.toString(2);

The following is an example −

Example

 Live Demo

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);
   }
}

Output

ByteArray to Binary = 10000000000000000

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 29-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements