Java Utililty Methods BigInteger to

List of utility methods to do BigInteger to

Description

The list of methods to do BigInteger to are organized into topic(s).

Method

BigIntegerconvertPositive(BigInteger bi)
convert Positive
if (bi.signum() < 0) {
    bi = new BigInteger(1, bi.toByteArray());
return bi;
BigIntegerconvertRealNumberBits(BigInteger source, int size, int direction)
convert Real Number Bits
return source;
BigIntegerconvertRepresentation(BigInteger b)
convert Representation
BigInteger actual = b.mod(p);
if (actual.compareTo(p_half) > 0) {
    actual = actual.subtract(p);
return actual;
StringconvertToBase62String(BigInteger value)
convert To Base String
StringBuilder sb = new StringBuilder();
while (true) {
    BigInteger mod = value.mod(BASE);
    sb.insert(0, elements[mod.intValue()]);
    value = value.divide(BASE);
    if (value.equals(BigInteger.ZERO)) {
        break;
return sb.toString();
longconvertToLong(BigInteger bigInteger)
convert To Long
if (bigInteger.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) == 1) {
    return Long.MAX_VALUE;
} else {
    return bigInteger.longValue();
StringconvertToString(BigInteger biSequence, int kmerSize)
convert To String
String str = "";
for (int i = 0; i < kmerSize; i++) {
    int idx = biSequence.mod(BigInteger.valueOf(4)).intValue();
    if (idx == 0) {
        str = "A" + str;
    } else if (idx == 1) {
        str = "C" + str;
    } else if (idx == 2) {
...
BigIntegerconvertValueToZeroIfNullOrNegative(BigInteger value)
convert Value To Zero If Null Or Negative
if (value == null || value.compareTo(BigInteger.ZERO) < 0) {
    return BigInteger.ZERO;
return value;
StringgetUnsignedBigIntegerAsHex(BigInteger bi)
get Unsigned Big Integer As Hex
String hexString = bi.toString(16);
if (hexString.length() < 16) {
    String zeroes = "000000000000000";
    hexString = zeroes.substring(0, 16 - hexString.length()) + hexString;
return hexString.toUpperCase();
byte[]getUnsignedBytes(BigInteger number, int length)
Returns an unsigned byte[] representation of the given big integer.
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;
...
StringstringForBig(BigInteger big, int sigchars)
string For Big
char[] chars = new char[sigchars];
for (int i = 0; i < sigchars; i++) {
    int maskpos = 16 * (sigchars - (i + 1));
    chars[i] = hashChars.charAt(
            big.and(CHAR_MASK.shiftLeft(maskpos)).shiftRight(maskpos).intValue() % hashChars.length());
return new String(chars);