import static java.lang.Math.floor;
import static java.lang.Math.pow;

public class NumberConversion {
    private static int charToValue(char s) {
        if(s - '0' >= 0 && '9' - s >= 0) {
            return s - '0';
        } else {
            return 10 + s - 'A';
        }
    }

    private static char valueToChar(int s) {
        if(s < 10) {
            return (char)('0' + s);
        } else {
            return (char)('A' + s - 10);
        }
    }

    private static long getDecimal(String number, int sys) {
        long result = 0;
        for(int i =  0; i < number.length(); i++){
            result += (long) (charToValue(number.charAt(i)) * pow(sys, number.length()-i-1));
        }
        return result;
    }

    private static String decimalToOther(long number, int sys) {
        if(number == 0) {
            return "0";
        } else {
            int o = 0;
            while (pow(sys, o) <= number) {
                o++;
            }
            o--;
            String result = "";
            for(int h = o; h >= 0; h--) {
                long pot = (long) pow(sys, h);
                int wieoft = (int) floor((double) number / (double) pot);
                number -= wieoft * pot;
                result += valueToChar(wieoft);
            }
            return result;
        }
    }

    private static String compute(int baseA, int baseB, String number) {
        long value = getDecimal(number, baseA);
        return decimalToOther(value, baseB);
    }

    public static void main(String[] args) {
        System.out.println("20419(10) -> 15 : " + compute(10, 15, "20419") + " --- " + Integer.toString(20419, 15));
    }
}
