Java Program to retrieve the current bits in a byte array in two’s-complement form

Java 8Object Oriented ProgrammingProgramming

Use the toByteArray() method to return a byte array containing the two's-complement representation of this BigInteger. The byte array will be in big-endian byte-order: the most significant byte is in the zeroth element.

The following is an example to retrieve the current bits in a byte array in two’s-complement form.

Example

 Live Demo

import java.math.*;
public class Demo {
   public static void main(String[] args) {
      // BigInteger objects
      BigInteger bi1, bi2;
      byte b1[] = { 0x1, 0x00, 0x00 };
      bi1 = new BigInteger(b1);
      b1 = bi1.toByteArray();
      String strResult = "Byte array representation of " + bi1 + " = ";
      System.out.println(strResult);
      for (int i = 0; i < b1.length; i++) {
         System.out.format("0x%02X\n", b1[i]);
      }
   }
}

Output

Byte array representation of 65536 =
0x01
0x00
0x00
raja
Published on 20-Dec-2018 08:14:27
Advertisements