private static char[] CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); /** * 转字符串 * @param num * @return */ public static String encode(long num) { StringBuffer stringBuffer = new StringBuffer(); if(num == 0) { stringBuffer.append(CHARS[0]); } while(num > 0) { stringBuffer.append(CHARS[(int) (num % 36)]); num = num / 36; } return stringBuffer.reverse().toString(); } /** * 转数值 * @param code * @return */ public static long decode(String code) { Map<Character, Integer> map = new HashMap<Character, Integer>(); for (int i = 0; i < CHARS.length; i++) { map.put(CHARS[i], i); } int size = code.length(); long num = 0; for(int i = 0; i<size; i++) { String char2str = String.valueOf(code.charAt(i)).toUpperCase(); num = (long) (map.get(char2str.charAt(0)) * Math.pow(36, size - i - 1) + num); } return num; }