package com.nbclass.util;

import java.util.Stack;

/**
 * 
 * @author Leon
 * @datetime 2019年4月11日 下午3:40:42
 */
public class ShortenUtil {

	private static final char[] array = {
    	'a', 'z', 'm', 'b', 'v', 'o', 'h', 'c', 'w', 'k', 'y', 'i', 'f', 's', 'l', 't', 'd', 'x', 'n', 'g', 'r', 'p', 'e', 'u', 'q', 'j',
    	'0', '8', '4', '9', '5', '2', '6', '3', '7', '1',
    	'P', 'G', 'A', 'Z', 'R', 'I', 'F', 'C', 'T', 'W', 'O', 'J', 'U', 'Y', 'E', 'N', 'V', 'H', 'B', 'M', 'S', 'L', 'X', 'D', 'Q', 'K'
	};
	
    //private static final char[] array1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray(); 
    
	public static void main(String[] args) {
		long id = 200;
		String str = encode(id);
		System.out.println("62System=" +str);  
        System.out.println("10System=" +decode(str));  
    }
	
	/**
	 * _10_to_62
	 * @param number
	 * @return
	 */
    public static String encode(long number){
    	number = 500000+number;
        Long rest=number;
        Stack<Character> stack=new Stack<Character>();
        StringBuilder result=new StringBuilder(0);
        while(rest!=0){
            stack.add(array[new Long((rest-(rest/62)*62)).intValue()]);
            rest=rest/62;
        }
        for(;!stack.isEmpty();){
            result.append(stack.pop());
        }
        return result.toString();
    }
    
    /** 
     * _10_to_62
     * 将10进制转化为62进制  
     * @param number  
     * @param length 转化成的62进制长度，不足length长度的话高位补0，否则不改变什么 
     * @return 
     */
    public static String encode(long number, int length){
         Long rest=number;  
         Stack<Character> stack=new Stack<Character>();  
         StringBuilder result=new StringBuilder(0);  
         while(rest!=0){  
             stack.add(array[new Long((rest-(rest/62)*62)).intValue()]);  
             rest=rest/62;  
         }  
         for(;!stack.isEmpty();){  
             result.append(stack.pop());  
         }  
         int result_length = result.length();  
         StringBuilder temp0 = new StringBuilder();  
         for(int i = 0; i < length - result_length; i++){  
             temp0.append('0');  
         }  
         return temp0.toString() + result.toString();  
    }

    public static long decode(String sixty_str){
        int multiple=1;
        long result=0;
        Character c;
        for(int i=0;i<sixty_str.length();i++){
            c=sixty_str.charAt(sixty_str.length()-i-1);
            result+=_62_value(c)*multiple;
            multiple=multiple*62;
        }
        return result;
    }

    private static int _62_value(Character c){
        for(int i=0;i<array.length;i++){
            if(c==array[i]){
                return i;
            }
        }
        return -1;
    }
    
    //------------------------------------------------------
    
    /** 
     * 将62进制转换成10进制数 
     *  
     * @param ident62 
     * @return 
     */  
    public static String convertBase62ToDecimal( String ident62 ) {
        int decimal = 0;  
        int base = 62;  
        int keisu = 0;  
        int cnt = 0;  

        byte ident[] = ident62.getBytes();  
        for ( int i = ident.length - 1; i >= 0; i-- ) {  
            int num = 0;  
            if ( ident[i] > 48 && ident[i] <= 57 ) {  
                num = ident[i] - 48;  
            }  
            else if ( ident[i] >= 65 && ident[i] <= 90 ) {  
                num = ident[i] - 65 + 10;  
            }  
            else if ( ident[i] >= 97 && ident[i] <= 122 ) {  
                num = ident[i] - 97 + 10 + 26;  
            }  
            keisu = (int) java.lang.Math.pow( base, cnt );  
            decimal += num * keisu;  
            cnt++;  
        }  
        return String.format( "%08d", decimal );  
    }
    
}
