Java BigInteger Calculate encodeToString(final BigInteger input)

Here you can find the source of encodeToString(final BigInteger input)

Description

Big integer to much smaller string encoding

License

Open Source License

Parameter

Parameter Description
input a parameter

Declaration

private static String encodeToString(final BigInteger input) 

Method Source Code

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

import java.math.BigInteger;

public class Main {
    private static final String ENCODE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789";

    /**//  w  w w  . j ava  2  s  .  co m
     * Represent bytes (from a hash) as a nice printable string
     *
     * @param mdbytes
     * @return
     */
    public static String encodeToString(final byte[] mdbytes) {
        return encodeToString(new BigInteger(1, mdbytes));
    }

    /**
     * Big integer to much smaller string encoding
     *
     * @param input
     * @return
     */
    private static String encodeToString(final BigInteger input) {
        final BigInteger encodeLength = BigInteger.valueOf(ENCODE.length());
        BigInteger running = input;
        BigInteger mod;
        final StringBuilder result = new StringBuilder(64);
        result.append(running.intValue() == 0 ? "0" : "");
        while (running.longValue() != 0) {
            mod = running.remainder(encodeLength);
            result.append(ENCODE.substring(mod.intValue(), mod.intValue() + 1));
            running = running.divide(encodeLength);
        }
        return result.reverse().toString();
    }
}

Related

  1. encodeCompactBits(BigInteger value)
  2. encodeFloat(BigInteger j, int e)
  3. encodeInteger(BigInteger i, OutputStream os)
  4. encodeMPI(BigInteger value, boolean includeLength)
  5. encodeToBase62(BigInteger num)
  6. encodeURL(BigInteger id)
  7. equal(BigInteger int1, BigInteger int2)
  8. equals(BigInteger[] a, BigInteger[] b)
  9. extractBoolean(BigInteger bi, int index)