Home > Java > javaTutorial > Invitation code generator Java code

Invitation code generator Java code

大家讲道理
Release: 2016-11-11 11:03:33
Original
2339 people have browsed it

Invitation code generator, which can reversely calculate the user ID based on the invitation code

import java.util.Random;
 
/**
 * 邀请码生成器,算法原理:<br/>
 * 1) 获取id: 1127738 <br/>
 * 2) 使用自定义进制转为:gpm6 <br/>
 * 3) 转为字符串,并在后面加&#39;o&#39;字符:gpm6o <br/>
 * 4)在后面随机产生若干个随机数字字符:gpm6o7 <br/>
 * 转为自定义进制后就不会出现o这个字符,然后在后面加个&#39;o&#39;,这样就能确定唯一性。最后在后面产生一些随机字符进行补全。<br/>
 * @author jiayu.qiu
 */
public class ShareCodeUtil {
 
    /** 自定义进制(0,1没有加入,容易与o,l混淆) */
    private static final char[] r=new char[]{&#39;q&#39;, &#39;w&#39;, &#39;e&#39;, &#39;8&#39;, &#39;a&#39;, &#39;s&#39;, &#39;2&#39;, &#39;d&#39;, &#39;z&#39;, &#39;x&#39;, &#39;9&#39;, &#39;c&#39;, &#39;7&#39;, &#39;p&#39;, &#39;5&#39;, &#39;i&#39;, &#39;k&#39;, &#39;3&#39;, &#39;m&#39;, &#39;j&#39;, &#39;u&#39;, &#39;f&#39;, &#39;r&#39;, &#39;4&#39;, &#39;v&#39;, &#39;y&#39;, &#39;l&#39;, &#39;t&#39;, &#39;n&#39;, &#39;6&#39;, &#39;b&#39;, &#39;g&#39;, &#39;h&#39;};
 
    /** (不能与自定义进制有重复) */
    private static final char b=&#39;o&#39;;
 
    /** 进制长度 */
    private static final int binLen=r.length;
 
    /** 序列最小长度 */
    private static final int s=6;
 
    /**
     * 根据ID生成六位随机码
     * @param id ID
     * @return 随机码
     */
    public static String toSerialCode(long id) {
        char[] buf=new char[32];
        int charPos=32;
 
        while((id / binLen) > 0) {
            int ind=(int)(id % binLen);
            // System.out.println(num + "-->" + ind);
            buf[--charPos]=r[ind];
            id /= binLen;
        }
        buf[--charPos]=r[(int)(id % binLen)];
        // System.out.println(num + "-->" + num % binLen);
        String str=new String(buf, charPos, (32 - charPos));
        // 不够长度的自动随机补全
        if(str.length() < s) {
            StringBuilder sb=new StringBuilder();
            sb.append(b);
            Random rnd=new Random();
            for(int i=1; i < s - str.length(); i++) {
            sb.append(r[rnd.nextInt(binLen)]);
            }
            str+=sb.toString();
        }
        return str;
    }
 
    public static long codeToId(String code) {
        char chs[]=code.toCharArray();
        long res=0L;
        for(int i=0; i < chs.length; i++) {
            int ind=0;
            for(int j=0; j < binLen; j++) {
                if(chs[i] == r[j]) {
                    ind=j;
                    break;
                }
            }
            if(chs[i] == b) {
                break;
            }
            if(i > 0) {
                res=res * binLen + ind;
            } else {
                res=ind;
            }
            // System.out.println(ind + "-->" + res);
        }
        return res;
    }
}
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template