Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.util.Arrays;

public class Main {
    static String toBase58(final byte[] in) {

        final int[] indexes = new int[128];

        Arrays.fill(indexes, -1);

        final char[] ab = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();

        for (int k = 0; k < ab.length; ++k)
            indexes[ab[k]] = k;

        int zeroCounter = 0;

        while (zeroCounter < in.length && in[zeroCounter] == 0)
            ++zeroCounter;

        final byte[] cp = Arrays.copyOf(in, in.length);

        final char[] enc = new char[cp.length * 2];

        int resBegin = enc.length;
        int begin = zeroCounter;

        while (begin < cp.length) {

            int rem = 0;
            for (int j = begin; j < cp.length; ++j) {
                final int temp = ((int) cp[j] & 0xFF) + (256 * rem);
                cp[j] = (byte) (temp / 58);
                rem = temp % 58;
            }

            enc[--resBegin] = ab[(byte) rem];
            if (cp[begin] == 0)
                ++begin;
        }

        while (resBegin < enc.length && ab[0] == enc[resBegin])
            ++resBegin;

        while (--zeroCounter >= 0)
            enc[--resBegin] = ab[0];

        return new String(enc, resBegin, enc.length - resBegin);
    }
}