BigInteger toByteArray() in Java

toByteArray(): This method is available in java.math.BigInteger class of Java.

Syntax:

byte[] java.math.BigInteger.toByteArray()

This method returns 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.

Returns: a byte array containing the two's-complement representation of this BigInteger.

For Example:

BigInteger bigInteger = new BigInteger("1234")

bigInteger.toByteArray() = > It returns [4, -46].

Approach

Java

import java.math.BigInteger;
import java.util.Arrays;

public class BigIntegertoByteArray {
    public static void main(String[] args) {

        BigInteger bigInteger = new BigInteger("1234");
        byte[] arr = bigInteger.toByteArray();

        System.out.println(Arrays.toString(arr));
    }
}

Output:

[4, -46]

No comments:

Post a Comment