Java BigInteger to getUnsignedBytes(BigInteger number, int length)

Here you can find the source of getUnsignedBytes(BigInteger number, int length)

Description

Returns an unsigned byte[] representation of the given big integer.

License

Open Source License

Parameter

Parameter Description
number The BigInteger. Must be >= 0.
length The maximum length.

Return

The last length bytes of the given big integer, filled with zeros if necessary.

Declaration

public static byte[] getUnsignedBytes(BigInteger number, int length) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.math.BigInteger;

public class Main {
    /**/*from w  w  w. j  ava2  s  .c  o  m*/
     * Returns an unsigned byte[] representation of the given big integer.
     * 
     * @param number
     *            The BigInteger. Must be >= 0.
     * @param length
     *            The maximum length.
     * @return The last <code>length</code> bytes of the given big integer,
     *         filled with zeros if necessary.
     */
    public static byte[] getUnsignedBytes(BigInteger number, int length) {
        byte[] value = number.toByteArray();

        if (value.length > length + 1) {
            throw new IllegalArgumentException(
                    "The given BigInteger does not fit into a byte array with the given length: " + value.length
                            + " > " + length);
        }

        byte[] result = new byte[length];

        int i = value.length == length + 1 ? 1 : 0;
        for (; i < value.length; i++) {
            result[i + length - value.length] = value[i];
        }

        return result;
    }
}

Related

  1. convertToBase62String(BigInteger value)
  2. convertToLong(BigInteger bigInteger)
  3. convertToString(BigInteger biSequence, int kmerSize)
  4. convertValueToZeroIfNullOrNegative(BigInteger value)
  5. getUnsignedBigIntegerAsHex(BigInteger bi)
  6. stringForBig(BigInteger big, int sigchars)
  7. toBase_128(BigInteger val)
  8. toByteArray(BigInteger i)
  9. toByteArray(final BigInteger b)