Java BigInteger Calculate trim(BigInteger n)

Here you can find the source of trim(BigInteger n)

Description

Treats the input as the MSB representation of a number, and lop off leading zero elements.

License

Open Source License

Parameter

Parameter Description
in the byte array to trim.

Return

the byte array with no leading 0-bytes.

Declaration

public static byte[] trim(BigInteger n) 

Method Source Code

//package com.java2s;
// the terms and conditions of the Cryptix General Licence. You should have

import java.math.BigInteger;

public class Main {
    /**//from   w  ww . ja  v a 2s  .c  om
     * Treats the input as the MSB representation of a number, and lop off
     * leading zero elements. For efficiency, the input is simply returned if no
     * leading zeroes are found.
     *
     * @param in the byte array to trim.
     * @return the byte array with no leading 0-bytes.
     */
    public static byte[] trim(BigInteger n) {
        byte[] in = n.toByteArray();
        if (in.length == 0 || in[0] != 0)
            return in;

        int len = in.length;
        int i = 1;
        while (in[i] == 0 && i < len)
            ++i;

        byte[] ret = new byte[len - i];
        System.arraycopy(in, i, ret, 0, len - i);
        return ret;
    }
}

Related

  1. sum(BigInteger valueA, BigInteger valueB)
  2. sum(BigInteger... values)
  3. sumOfDigits(BigInteger ab)
  4. sumOfDigits(BigInteger n)
  5. trim(BigInteger bi)
  6. trimToPrecision(BigInteger number, int precision)
  7. uint64ToByteStreamLE(BigInteger val, OutputStream stream)
  8. unsignedBigIntergerToByteArray(BigInteger bigInteger)
  9. unsignedLongToBigInteger(byte[] source)