Java BigInteger Calculate decodeToBigInteger(String input)

Here you can find the source of decodeToBigInteger(String input)

Description

decode To Big Integer

License

Apache License

Declaration

public static BigInteger decodeToBigInteger(String input) 

Method Source Code

//package com.java2s;
/*// w w  w  .  ja v a  2s .  c o m
 * Copyright 2014 http://Bither.net
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.math.BigInteger;

public class Main {
    private static final int[] INDEXES = new int[128];

    public static BigInteger decodeToBigInteger(String input) {
        return new BigInteger(1, decode(input));
    }

    public static byte[] decode(String input) {

        if (input.length() == 0) {
            return new byte[0];
        }
        // Get rid of any UTF-8 BOM marker. Those should not be present, but might have slipped in nonetheless,
        // since Java does not automatically discard them when reading a stream. Only remove it, if at the beginning
        // of the string. Otherwise, something is probably seriously wrong.
        if (input.charAt(0) == '\uFEFF')
            input = input.substring(1);

        byte[] input58 = new byte[input.length()];
        // Transform the String to a base58 byte sequence
        for (int i = 0; i < input.length(); ++i) {
            char c = input.charAt(i);

            int digit58 = -1;
            if (c >= 0 && c < 128) {
                digit58 = INDEXES[c];
            }
            if (digit58 < 0) {
                return null;
            }

            input58[i] = (byte) digit58;
        }
        // Count leading zeroes
        int zeroCount = 0;
        while (zeroCount < input58.length && input58[zeroCount] == 0) {
            ++zeroCount;
        }
        // The encoding
        byte[] temp = new byte[input.length()];
        int j = temp.length;

        int startAt = zeroCount;
        while (startAt < input58.length) {
            byte mod = divmod256(input58, startAt);
            if (input58[startAt] == 0) {
                ++startAt;
            }

            temp[--j] = mod;
        }
        // Do no add extra leading zeroes, move j to first non null byte.
        while (j < temp.length && temp[j] == 0) {
            ++j;
        }

        return copyOfRange(temp, j - zeroCount, temp.length);
    }

    private static byte divmod256(byte[] number58, int startAt) {
        int remainder = 0;
        for (int i = startAt; i < number58.length; i++) {
            int digit58 = (int) number58[i] & 0xFF;
            int temp = remainder * 58 + digit58;

            number58[i] = (byte) (temp / 256);

            remainder = temp % 256;
        }

        return (byte) remainder;
    }

    private static byte[] copyOfRange(byte[] source, int from, int to) {
        byte[] range = new byte[to - from];
        System.arraycopy(source, from, range, 0, range.length);

        return range;
    }
}

Related

  1. createUUID(int version, String gtin, BigInteger phar)
  2. cryptRSA(byte[] data, BigInteger exponent, BigInteger modulus)
  3. decodeBigInteger(String value)
  4. decodeBitListFromBigInteger(BigInteger bits)
  5. decodeFloat(BigInteger rawSign, BigInteger rawExponent, BigInteger rawMantissa, int signWidth, int exponentWidth, int mantissaWidth, int bias, MathContext mc)
  6. DiffieHellman(BigInteger p, BigInteger g, BigInteger x, BigInteger y, BigInteger y_B)
  7. distance(BigInteger a, BigInteger b)
  8. distance(BigInteger a, BigInteger b)
  9. div(BigInteger dividend, BigInteger divisor)