// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class RadixFun3 {
    private static String DIGITS = "0123456789abcdefghijklmnpqrstuvwxyzWXYZ";
    private static int fromAny(String s, int radix, int[] z) {
        int res = 0;
        char[] sa = s.toCharArray();
        int sign = 1;
        boolean fz = true;
        for (int i = 0; i < s.length(); i++) {
            if(fz && sa[i] == '0') {
                z[0]++;
            } else if(sa[i] != '-') {
                res = res * radix + DIGITS.indexOf(sa[i]);
                fz = false;
            } else {
                sign = -1;
            }
        }
        return sign * res;
    }
    private static String toAny(int i, int radix, int[] z) {
        if(i >= 0) {
            String res = "";
            int tmp = i;
            while (tmp > 0) {
                res = DIGITS.toCharArray()[tmp % radix] + res;
                tmp = tmp / radix;
            }
            for(int k = 0; k < z[0]; k++) res = '0' + res;
            return res;
        } else {
            return "-" + toAny(-i, radix, z);
        }
    }
    public static String enc(String s) {
        int[] z = new int[1];
        return toAny(fromAny(s, 10, z), 36, z);
    }
    public static String dec(String s) {
        int[] z = new int[1];
        return toAny(fromAny(s, 36, z), 10, z);
    }
    public static void main(String[] args) {
        String s1 = "0526871";
        String s2 = enc(s1);
        String s3 = dec(s2);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
    }
}
