Java BigInteger Calculate copy_into_byte_array(BigInteger bi, byte[] a)

Here you can find the source of copy_into_byte_array(BigInteger bi, byte[] a)

Description

Copy a BigInteger into a byte array.

License

Open Source License

Parameter

Parameter Description
bi the source BigInteger
a the destination array

Declaration

public static void copy_into_byte_array(BigInteger bi, byte[] a) 

Method Source Code

//package com.java2s;
// modify it under the terms of the GNU General Public License as

import java.math.BigInteger;

public class Main {
    /**/*ww w  . j a  v a 2 s . c om*/
     * 
     * Copy a BigInteger into a byte array. This is a bit more tricky
     * than just copying the result of {@link BigInteger#toByteArray}.
     * First, the argument array {@code a} might need some zero
     * padding and second, {@link BigInteger#toByteArray} might have a
     * leading zero that does not fit into {@code a}.
     * <P>
     *
     * Asserts that the BigInteger {@code bi}, with leading zeros
     * removed, fits into {@code a}.
     * 
     * @param bi the source BigInteger 
     * @param a the destination array
     */
    public static void copy_into_byte_array(BigInteger bi, byte[] a) {
        byte[] bia = bi.toByteArray();
        assert bia.length <= a.length || (bia.length == a.length + 1 && bia[0] == 0);
        for (int i = 0; i < a.length - bia.length; i++)
            a[i] = 0;
        System.arraycopy(bia, bia.length == a.length + 1 ? 1 : 0, a,
                bia.length == a.length + 1 ? 0 : a.length - bia.length,
                bia.length == a.length + 1 ? a.length : bia.length);
        return;
    }
}

Related

  1. calculateRx_(BigInteger Rx, BigInteger n)
  2. castToBigInteger(Object val)
  3. chop(BigInteger in, int bits)
  4. clip(BigInteger bi, int numberOfBytes)
  5. clone(BigInteger[] data)
  6. copyOf(BigInteger[] data, int newLength)
  7. copyOfRange(BigInteger[] data, int from, int to)
  8. createUnsignedBigInteger(byte[] data)
  9. createUUID(int version, String gtin, BigInteger phar)