Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.math.BigInteger;

public class Main {
    final protected static String base58Array = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

    public static String BytesToBase58(byte[] value) {
        //Add 1 for each 00 byte.
        //From lowest base58 fill with division remainders.
        String returnValue = "";
        boolean justStarted = true;
        BigInteger bigValue = new BigInteger(value); //TODO: Check that it works as it should.
        BigInteger base58 = new BigInteger("58");
        BigInteger zero = new BigInteger("0");
        BigInteger[] divisionResult;
        while (bigValue.compareTo(zero) == 1) { //Means greater than.
            divisionResult = bigValue.divideAndRemainder(base58);
            bigValue = divisionResult[0];
            returnValue = base58Array.toCharArray()[divisionResult[1].intValue()] + returnValue;
        }
        for (int i = 0; i < value.length; i++) {
            if (value[i] == 0 && justStarted) {
                returnValue = "1" + returnValue;
            } else {
                break;
            }
            justStarted = false;
        }
        return returnValue;
    }
}