派生自 projectDept/qhighschool

yn147
2023-05-10 96286178ee1c257c130cb2ad964a781f36c4eee5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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 ;
    }
    
}