Java BigInteger to toBase_128(BigInteger val)

Here you can find the source of toBase_128(BigInteger val)

Description

This is based on bit shifting (using base128()).

License

Open Source License

Parameter

Parameter Description
val a parameter

Declaration

public static byte[] toBase_128(BigInteger val) 

Method Source Code

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

import java.math.BigInteger;
import java.util.ArrayList;

public class Main {
    public final static BigInteger BN127 = new BigInteger("127");

    /**/*from   w w  w  . j a va  2 s. c o m*/
     * This is based on bit shifting (using base128()).
     * Returns array of bytes.
     * @param val
     * @return
     */
    public static byte[] toBase_128(BigInteger val) {
        ArrayList<Integer> al = base128(val);
        byte[] result = new byte[al.size()];
        for (int k = 0; k < result.length; k++)
            result[k] = al.get(k).byteValue();
        ;
        return result;
    }

    /**
     * Convert to base 128 (bigendian), using shifts.
     * @param val
     * @return
     */
    public static ArrayList<Integer> base128(BigInteger val) {
        ArrayList<Integer> result = new ArrayList<Integer>();
        int part = val.and(BN127).intValue();
        val = val.shiftRight(7);
        result.add(0, new Integer(part));
        while (!val.equals(BigInteger.ZERO)) {
            part = val.and(BN127).intValue();
            val = val.shiftRight(7);
            part += 128;
            result.add(0, new Integer(part));
        }
        ;
        return result;
    }
}

Related

  1. convertToString(BigInteger biSequence, int kmerSize)
  2. convertValueToZeroIfNullOrNegative(BigInteger value)
  3. getUnsignedBigIntegerAsHex(BigInteger bi)
  4. getUnsignedBytes(BigInteger number, int length)
  5. stringForBig(BigInteger big, int sigchars)
  6. toByteArray(BigInteger i)
  7. toByteArray(final BigInteger b)
  8. toByteArray(final BigInteger value, final int numBytes)
  9. toByteArrayUnsigned(BigInteger bi)