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


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
", b1[i]);       }    } }

Output

Byte array representation of 65536 =
0x01
0x00
0x00

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 29-Jun-2020

197 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements