long number to Base - Java java.lang

Java examples for java.lang:long

Description

long number to Base

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        long number = 2;
        int base = 2;
        System.out.println(toBase(number, base));
    }//from www .ja va2s .c  o  m

    private static String alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static String toBase(long number, int base) {
        if (number < base) {
            return Character.toString(alphabet.charAt((int) number));
        }

        long value = number;
        StringBuffer sb = new StringBuffer();

        while (value != 0) {
            int remind = (int) (value % base);
            value = (value - remind) / base;
            sb.append(alphabet.charAt(remind));
        }

        return sb.toString();
    }
}

Related Tutorials