Java BigInteger to toMinimalByteArray(BigInteger value)

Here you can find the source of toMinimalByteArray(BigInteger value)

Description

Return the value of big as a byte array.

License

Open Source License

Parameter

Parameter Description
value the <tt>BigInteger</tt> value to be converted to a byte array

Return

the value big as byte array

Declaration

public static byte[] toMinimalByteArray(BigInteger value) 

Method Source Code

//package com.java2s;
import java.math.BigInteger;

public class Main {
    /**//  w  w w  .j a v  a2 s .c  o  m
     * Return the value of <tt>big</tt> as a byte array. Although BigInteger
     * has such a method, it uses an extra bit to indicate the sign of the
     * number. For elliptic curve cryptography, the numbers usually are
     * positive. Thus, this helper method returns a byte array of minimal
     * length, ignoring the sign of the number.
     *
     * @param value the <tt>BigInteger</tt> value to be converted to a byte
     *              array
     * @return the value <tt>big</tt> as byte array
     */
    public static byte[] toMinimalByteArray(BigInteger value) {
        byte[] valBytes = value.toByteArray();
        if ((valBytes.length == 1) || (value.bitLength() & 0x07) != 0) {
            return valBytes;
        }
        byte[] result = new byte[value.bitLength() >> 3];
        System.arraycopy(valBytes, 1, result, 0, result.length);
        return result;
    }
}

Related

  1. toInt(int length, BigInteger bi)
  2. toIntArray(BigInteger[] input)
  3. toInteger(BigInteger bi)
  4. toInteger(BigInteger integer)
  5. toIntegerBytes(final BigInteger bigInt)
  6. toObject(BigInteger x)
  7. toPaddedBytes(BigInteger bi, int length)
  8. toPushbackReader(BigInteger x)
  9. toStr(BigInteger number)