Get byte array from BigInteger in Java


First, set the BigInteger object with binary.

BigInteger val = new BigInteger("100000000110001100000", 2);

Now, use the toByteArray() method.

byte[] byteArr = val.toByteArray();

The following is an example −

Example

 Live Demo

import java.math.BigInteger;
public class Demo {
   public static void main(String[] argv) throws Exception {
      BigInteger val = new BigInteger("100000000110001100000", 2);
      byte[] byteArr = val.toByteArray();
      for (int i = 0; i < byteArr.length; i++) {
         System.out.format("0x%02X
", byteArr[i]); } } }

Output

0x10
0x0C
0x60

Updated on: 30-Jul-2019

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements