package com.qxueyou.scc.base.util;
|
|
|
|
public class UserCodeUtils {
|
|
public static final String allChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
// 0 - 9 对应ascii值为 48 --> 57
|
// A - Z 对应ascii值为 65 --> 90
|
// a - z 对应ascii值为 97 --> 122
|
|
public static void main(String[] args) {
|
|
long index = 56800235585L ;
|
|
generateCode(6,index);
|
|
}
|
|
/**
|
* 生成code
|
* @param length
|
* @param index
|
* @return
|
*/
|
public static String generateCode(int length,long index){
|
|
String code = "";
|
|
int[] allIndex = generateCodeIndex(length,index);
|
|
for(int i = 0 ; i < length; i++) {
|
|
code = code + allChars.charAt(allIndex[5-i]);
|
|
}
|
|
return code ;
|
|
}
|
|
/**
|
* 生成每个位置上的序号
|
* @param length
|
* @param index
|
* @return
|
*/
|
private static int[] generateCodeIndex(int length,long index){
|
|
int[] result = new int[]{0,0,0,0,0,0};
|
|
for(int loopTime = 0 ; loopTime < length ; loopTime ++ ){
|
|
result[loopTime] = (int)(index % 62) ;
|
|
index = index / 62 ;
|
|
}
|
|
return result ;
|
}
|
|
}
|