邀请码工具类

package org.colin.utils;

import java.util.Arrays;

/**
 * 邀请码工具类
 * @author Colin
 */
public class InvitationCodeUtils {

    private static final String[] M1 = { "A", "W", "Z", "J", "M", "N", "7", "U", "S", "C", "8", "R", "F", "4", "9", "G",
            "H", "D", "Y", "K", "6", "V", "3", "B", "2", "E", "T", "L", "P", "Q", "5", "X" };
    private static final String[] M2 = { "T", "V", "G", "Z", "J", "W", "4", "L", "Q", "N", "K", "Y", "2", "5", "9", "D",
            "H", "E", "B", "X", "S", "A", "M", "8", "U", "7", "3", "C", "P", "R", "F", "6" };
    private static final String[] M3 = { "9", "K", "R", "V", "J", "S", "M", "N", "W", "C", "U", "D", "X", "Q", "8", "5",
            "H", "F", "B", "A", "L", "2", "P", "6", "3", "Y", "Z", "T", "7", "G", "E", "4" };
    private static final String[] M4 = { "E", "F", "U", "G", "A", "9", "8", "L", "M", "5", "7", "X", "S", "R", "K", "6",
            "T", "N", "3", "W", "V", "P", "B", "2", "4", "D", "Z", "Y", "H", "J", "C", "Q" };
    private static final String[] M5 = { "N", "C", "P", "J", "U", "3", "2", "T", "L", "S", "9", "W", "Y", "5", "B", "7",
            "H", "X", "R", "M", "4", "Z", "K", "E", "Q", "V", "F", "D", "A", "G", "6", "8" };
    private static final String[] M6 = { "F", "P", "9", "S", "C", "B", "7", "T", "U", "E", "A", "M", "N", "K", "D", "R",
            "Z", "X", "H", "5", "W", "3", "V", "G", "2", "4", "J", "6", "Q", "Y", "8", "L" };

    /**
     * 邀请码生成器
     * @param id 大于等于1且小于等于32^6
     * @return id相对应的6位32进制邀请码
     */
    public static final String generate(long id) {
        if (id < 1 || id > Math.pow(32, 6)) {
            throw new RuntimeException("生成邀请码失败>>参数值不在规定的范围内");
        }
        int i1, i2, i3, i4, i5, i6;
        i1 = (int) ((id - 1) / Math.pow(32, 5));
        i6 = (int) ((id - 1) % Math.pow(32, 5));
        i2 = (int) (i6 / Math.pow(32, 4));
        i6 = (int) (i6 % Math.pow(32, 4));
        i3 = (int) (i6 / Math.pow(32, 3));
        i6 = (int) (i6 % Math.pow(32, 3));
        i4 = (int) (i6 / Math.pow(32, 2));
        i6 = (int) (i6 % Math.pow(32, 2));
        i5 = i6 / 32;
        i6 = i6 % 32;
        return M1[i1] + M2[i2] + M3[i3] + M4[i4] + M5[i5] + M6[i6];
    }

    /**
     * 邀请码解析器
     * @param code 6位32进制邀请码
     * @return 邀请码对应的id
     */
    public static final long parse(String code) {
        if (code.length() != 6) {
            throw new RuntimeException("解析验证码失败>>无效的邀请码");
        }
        String[] arr = code.split("");
        int i1, i2, i3, i4, i5, i6;
        i1 = Arrays.asList(M1).indexOf(arr[0]);
        i2 = Arrays.asList(M2).indexOf(arr[1]);
        i3 = Arrays.asList(M3).indexOf(arr[2]);
        i4 = Arrays.asList(M4).indexOf(arr[3]);
        i5 = Arrays.asList(M5).indexOf(arr[4]);
        i6 = Arrays.asList(M6).indexOf(arr[5]);
        if (i1 == -1 || i2 == -1 || i3 == -1 || i4 == -1 || i5 == -1 || i6 == -1) {
            throw new RuntimeException("解析验证码失败>>无效的邀请码");
        }
        return (long) (i1 * Math.pow(32, 5) + i2 * Math.pow(32, 4) + i3 * Math.pow(32, 3) + i4 * Math.pow(32, 2)
                + i5 * 32 + i6 + 1);
    }
}
时间: 2024-10-04 00:35:02

邀请码工具类的相关文章

Java使用Zxing生成、解析二维码工具类

Zxing是Google提供的关于条码(一维码.二维码)的解析工具,提供了二维码的生成与解析的方法. 1.二维码的生成 (1).将Zxing-core.jar 包加入到classpath下. (2).二维码的生成需要借助MatrixToImageWriter类,该类是由Google提供的; package com.qlwb.business.util; //导入省略... /** * 二维码工具类 * */ public class MatrixToLogoImageWriter { priva

二维码工具类 - QrcodeUtils.java

二维码工具类,提供多种生成二维码.解析二维码的方法,包括中间logo的二维码等方法. 源码如下:(点击下载 - QrcodeUtils.java.MatrixToImageWriterEx.java.MatrixToLogoImageConfig.java.commons-io-2.4.jar.commons-lang-2.6.jar.slf4j-api-1.7.12.jar.javase-3.1.0.jar.core-3.1.0.jar .FolderUtils.java) QrcodeUti

生成二维码工具类

生成二维码工具类: public Bitmap CreateTwoDCode(String content) throws Exception { // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败 BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300); int width = matrix.getWidth(); int hei

Android使用zxing(二维码工具类)类库的导入方式

zXing导入比较麻烦,很多新人要导入很久,关键是还不一定导入的对.下面总结下导入方式.希望对大家有所帮助.

java 二维码 工具类

private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; private MatrixToImageWriter() {} public static BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.getHeight();

Java版本logo 名片二维码生成工具类

 package com.zzq; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Shape; import java.awt.font.FontRenderContext; import java.awt.ge

二维码生成工具类java版

注意:这里我不提供所需jar包的路径,我会把所有引用的jar包显示出来,大家自行Google package com.net.util; import java.awt.BasicStroke; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Shape; import java.awt.geom.RoundRectangle2D; import java.a

Java生成带小图标的二维码-google zxing 工具类

近期一直忙于开发微信商城项目,应客户要求,要开发个有图标的二维码.经过两次改版,终于实现了该功能(第一次没有小图标,这次才整合好的),如下是完整代码 . 该代码使用Java7开发,另外使用 core-2.2.jar jar[http://pan.baidu.com/s/1skTwHQ1] 包 . 1 package com.rick.common.utils; 2 3 4 import java.awt.Color; 5 import java.awt.Graphics2D; 6 import

非专业码农 JAVA学习笔记 6java工具类和算法-string

续<非专业码农 JAVA学习笔记 5 java工具类和算法> 五.字符串string 字符串和字符的差别:字符串双引号括起来”n”,字符用单引号括起来,表示一种符号’\n’ 1.string的主要方法和属性 类 方法或者属性 备注 定义string Stirng s=new string(“值”),string s=”值” 属性 string.length:string的长度为字节 方法startswith,endswith s.startwith(“值”)-以值为开头,s.endswith(