Java BigInteger Calculate getByteArrayFromBigIntegerArray(Object value)

Here you can find the source of getByteArrayFromBigIntegerArray(Object value)

Description

Convert an instance of our value class into a byte[].

License

Open Source License

Parameter

Parameter Description
value Object to be converted

Return

converted byte array

Declaration

public static byte[] getByteArrayFromBigIntegerArray(Object value) 

Method Source Code

//package com.java2s;
/**********************************************************************
Copyright (c) 2004 Brendan de Beer and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at//  ww  w  . ja  v a2 s  . c o m
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
    
Contributors:
2004 Brendan de Beer - Initial contributor for conversion methods
2005 Erik Bengtson - refactor mapping
2005 Andy Jefferson - added Timestamp/String converters
...
**********************************************************************/

import java.math.BigInteger;

public class Main {
    /**
     * Convert an instance of our value class into a byte[].
     *
     * @param value Object to be converted
     *
     * @return converted byte array
     */
    public static byte[] getByteArrayFromBigIntegerArray(Object value) {
        if (value == null) {
            return null;
        }

        BigInteger[] a = (BigInteger[]) value;
        long[] d = new long[a.length];

        for (int i = 0; i < a.length; i++) {
            d[i] = a[i].longValue();
        }

        return getByteArrayFromLongArray(d);
    }

    /**
     * Convert an instance of our value class into a byte[].
     *
     * @param value Object to be converted
     *
     * @return converted byte array
     */
    public static byte[] getByteArrayFromLongArray(Object value) {
        if (value == null) {
            return null;
        }

        long[] a = (long[]) value;
        int n = a.length;
        byte[] buf = new byte[n * 8];
        int i = 0;
        int j = 0;

        for (; i < n;) {
            long x = a[i++];
            buf[j++] = (byte) ((x >>> 56) & 0xFF);
            buf[j++] = (byte) ((x >>> 48) & 0xFF);
            buf[j++] = (byte) ((x >>> 40) & 0xFF);
            buf[j++] = (byte) ((x >>> 32) & 0xFF);
            buf[j++] = (byte) ((x >>> 24) & 0xFF);
            buf[j++] = (byte) ((x >>> 16) & 0xFF);
            buf[j++] = (byte) ((x >>> 8) & 0xFF);
            buf[j++] = (byte) (x & 0xFF);
        }

        return buf;
    }
}

Related

  1. gcd(BigInteger... values)
  2. gcd(Iterable nums)
  3. gcdEuclides(BigInteger a, BigInteger b)
  4. gcdExtended(BigInteger p, BigInteger q)
  5. getAddressText(BigInteger address)
  6. getBytes(BigInteger bi, int minLen)
  7. getBytes(BigInteger big, int bitlen)
  8. getBytesWithoutSign(BigInteger arg)
  9. getClearExpPipe(BigInteger M)