Java BigInteger to bigIntegerToBytes(BigInteger n, byte[] data, int[] offset)

Here you can find the source of bigIntegerToBytes(BigInteger n, byte[] data, int[] offset)

Description

Write the bytes representing n into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.

License

Open Source License

Parameter

Parameter Description
n the <code>BigInteger</code> to encode
data The byte array to store into, or <code>null</code>.
offset A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.

Declaration

public static final void bigIntegerToBytes(BigInteger n, byte[] data, int[] offset) 

Method Source Code


//package com.java2s;
/*//from   w  w w  .  j  av a  2 s.  c  o m
 * JGrass - Free Open Source Java GIS http://www.jgrass.org 
 * (C) HydroloGIS - www.hydrologis.com 
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Library General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option) any
 * later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Library General Public License
 * along with this library; if not, write to the Free Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.math.BigInteger;

public class Main {
    public static final int SIZE_INT = 4;

    /**
     * Write the bytes representing <code>n</code> into the byte array
     * <code>data</code>, starting at index <code>offset [0]</code>, and
     * increment <code>offset [0]</code> by the number of bytes written; if
     * <code>data == null</code>, increment <code>offset [0]</code> by the
     * number of bytes that would have been written otherwise.
     *
     * @param n the <code>BigInteger</code> to encode
     * @param data The byte array to store into, or <code>null</code>.
     * @param offset A single element array whose first element is the index in
     *         data to begin writing at on function entry, and which on
     *         function exit has been incremented by the number of bytes
     *         written.
     */
    public static final void bigIntegerToBytes(BigInteger n, byte[] data, int[] offset) {
        byte[] bytes = n.toByteArray();
        intToBytes(bytes.length, data, offset);
        offset[0] += memcpy(data, offset[0], bytes, 0, bytes.length);
    }

    /**
     * Write the bytes representing <code>i</code> into the byte array
     * <code>data</code>, starting at index <code>offset [0]</code>, and
     * increment <code>offset [0]</code> by the number of bytes written; if
     * <code>data == null</code>, increment <code>offset [0]</code> by the
     * number of bytes that would have been written otherwise.
     *
     * @param i the <code>int</code> to encode
     * @param data The byte array to store into, or <code>null</code>.
     * @param offset A single element array whose first element is the index in
     *         data to begin writing at on function entry, and which on
     *         function exit has been incremented by the number of bytes
     *         written.
     */
    public static final void intToBytes(int i, byte[] data, int[] offset) {
        /**
         * TODO: We use network-order within OceanStore, but temporarily
         * supporting intel-order to work with some JNI code until JNI code is
         * set to interoperate with network-order.
         */
        if (data != null) {
            for (int j = (offset[0] + SIZE_INT) - 1; j >= offset[0]; --j) {
                data[j] = (byte) i;
                i >>= 8;
            }
        }

        offset[0] += SIZE_INT;
    }

    /**
     * Copy contents of one array of <code>bytes</code> into another. If either
     * array is <code>null</code>, simply return the <code>length</code>
     * parameter directly.
     *
     * @param dst the array to write, or <code>null</code>
     * @param dst_offset the start offset in <code>dst</code>
     * @param src the array to read, or <code>null</code>
     * @param src_offset the start offset in <code>src</code>
     * @param length the number of <code>byte</code>s to copy.
     *
     * @return DOCUMENT ME!
     */
    public static int memcpy(byte[] dst, int dst_offset, byte[] src, int src_offset, int length) {
        if ((dst != null) && (src != null)) {
            if (dst.length < (dst_offset + length)) {
                croak("dst.length = " + dst.length + ", but " + "dst_offset = " + dst_offset + " and length = "
                        + length + ".");
            }

            if (src.length < (src_offset + length)) {
                croak("src.length = " + src.length + ", but " + "src_offset = " + src_offset + " and length = "
                        + length + ".");
            }

            for (int i = 0; i < length; ++i, ++dst_offset, ++src_offset)
                dst[dst_offset] = src[src_offset];
        }

        return length;
    }

    /**
     * DOCUMENT ME!
     *
     * @param msg DOCUMENT ME!
     */
    private static void croak(String msg) {
        // throw new java.AssertionViolatedException(msg);
    }
}

Related

  1. bigIntegerToBytes(BigInteger b, int numBytes)
  2. bigIntegerToBytes(BigInteger b, int numBytes)
  3. bigIntegerToBytes(BigInteger b, int numBytes)
  4. bigIntegerToBytes(BigInteger b, int numBytes)
  5. bigIntegerToBytes(BigInteger integer)
  6. bigIntegerToBytes(final BigInteger bigInteger)
  7. bigIntegerToDigits(BigInteger n)
  8. BigIntegerToEightBytes(BigInteger value)
  9. bigIntegerToHex(final BigInteger bigInteger)