• IDCode校验算法


    运行地址:

    https://c.runoob.com/compile/10

    IDCODE校验器官网

    http://check.idcode.net/

    算法源码

    public class HelloWorld {
        public static void main(String []args) {
            String code = "MA.156.M0.100009.96006002.S1576130942.P190101.LABS20191201.E191201.V191201.C7";
            String[] codeList = code.split("\.");
            String code1 = "";
            String code2 = "";
            for (int i = 0; i < codeList.length; i++) {
                if (i <= 4) {
                    code1 = code1 + "." + codeList[i];
                } else if (i != codeList.length - 1) {
                    code2 = code2 + "." + codeList[i];
                }
            }
            //去掉最后一个校验位和前边的点
            code1 = code1.substring(1, code1.length() - 1);
            System.out.println("code:" + code);
            System.out.println("/*******************************/");
            System.out.println("code1:" + code1); 
            String code1Num = letter2num(code1) + "Y";
            System.out.println("code1Num:" + code1Num);
            code1Num = evenNum(code1Num);
            System.out.println("evenNum(code1Num):" + code1Num); 
            int result1 = sumNum(code1Num);
            System.out.println("result1:" + result1);
            int y = 10 - (result1 % 10);
            System.out.println("y:" + y);
            code1 = code1 + y;
            System.out.println(code1);
    
           
            System.out.println("/*******************************/");
            System.out.println("code2:" + code2);
            String code2Num = letter2num(code2) + "Z";
            System.out.println("code2Num:" + code2Num);
            code2Num = evenNum(code2Num);
            System.out.println("evenNum(code2Num):" +code2Num);
            int result2 = sumNum(code2Num);
            System.out.println("result2:" + result2);
            int z = 10 - (result2 % 10);
            System.out.println("z:" + z);
            System.out.println("/*******************************/");
            code2 = code2 + ".C" + z;
            System.out.println("code2:" + code2);
            System.out.println("LAST:" + code1 + code2);
    
        }
    
        private static String letter2num(String letterStr) {
            String num = "";
            for (int i = 0; i < letterStr.length(); i++) {
                for (letter e : letter.values()) {
                    if (String.valueOf(letterStr.charAt(i)).equals(e.getLetter())) {
                        num += e.getNum();
                    }
                }
            }
            return num;
        }
    
        private static String evenNum(String numStr) {
            String result = "";
            numStr = new StringBuffer(numStr).reverse().toString();
            for (int i = 1; i <= numStr.length(); i++) {
                char num = numStr.charAt(i - 1);
                if (i % 2 == 0) {
                    int numTemp = Integer.valueOf(String.valueOf(num));
                    numTemp = numTemp * 2;
                    if (numTemp > 9) {
                        String numS = String.valueOf(numTemp);
                        numTemp = Integer.valueOf(String.valueOf(numS.charAt(0))) + Integer.valueOf(String.valueOf(numS.charAt(1)));
                    }
                    result = result + numTemp;
                } else {
                    result = result + num;
                }
            }
            return result;
        }
    
        private static int sumNum(String result) {
            result = result.substring(1, result.length());
            int c = 0;
            int count = result.length();
            int[] a = new int[count];
            for (int i = 0; i < result.length(); i++) {
                a[i] = Integer.valueOf(String.valueOf(result.charAt(i)));
            }
    
            for (int i = 0; i < a.length; i++) {
                c += a[i];
            }
            return c;
        }
    
        private enum letter {
            To1("A", 10),
            To2("B", 11),
            To3("C", 12),
            To4("D", 13),
            To5("E", 14),
            To6("F", 15),
            To7("G", 16),
            To8("H", 17),
            To9("I", 18),
            To10("J", 19),
            To11("K", 20),
            To12("L", 21),
            To13("M", 22),
            To14("N", 23),
            To15("O", 24),
            To16("P", 25),
            To17("Q", 26),
            To18("R", 27),
            To19("S", 28),
            To20("T", 29),
            To21("U", 30),
            To22("V", 31),
            To23("W", 32),
            To24("X", 33),
            To25("Y", 34),
            To26("Z", 35),
            To27("a", 36),
            To28("b", 37),
            To29("c", 38),
            To30("d", 39),
            To31("e", 40),
            To32("f", 41),
            To33("g", 42),
            To34("h", 45),
            To35("i", 46),
            To36("j", 47),
            To37("k", 48),
            To38("l", 49),
            To39("m", 50),
            To40("n", 51),
            To41("o", 52),
            To42("p", 53),
            To43("q", 54),
            To44("r", 55),
            To45("s", 56),
            To46("t", 57),
            To47("u", 58),
            To48("v", 59),
            To49("w", 60),
            To50("x", 61),
            To51("y", 62),
            To52("z", 63),
            To53("0", 0),
            To54("1", 1),
            To55("2", 2),
            To56("3", 3),
            To57("4", 4),
            To58("5", 5),
            To59("6", 6),
            To60("7", 7),
            To61("8", 8),
            To63("-", 64),
            To64(".", 65),
            To65("@", 66),
            To66("$", 67),
            To67(",", 68),
            To68("*", 69),
            To69("+", 70),
            To70("%", 71),
            To71("/", 72),
            To72("#", 73),
            To73("!", 74),
            To74("^", 75),
            To75("~", 76);
            private String letter;
            private Integer num;
    
            letter(String letter, Integer num) {
                this.letter = letter;
                this.num = num;
            }
    
            public String getLetter() {
                return letter;
            }
    
            public Integer getNum() {
                return num;
            }
        }
    }

    输出

    code:MA.156.M0.100009.96006002.S1576130942.P190101.LABS20191201.E191201.V191201.C7
    /*******************************/
    code1:MA.156.M0.100009.9600600
    code1Num:22106515665220651000065600600Y
    evenNum(code1Num):Y00300616000025304216352530224
    result1:65
    y:5
    MA.156.M0.100009.96006005
    /*******************************/
    code2:.S1576130942.P190101.LABS20191201.E191201.V191201
    code2Num:652815761304265251010165211011282011201651411201653111201Z
    evenNum(code2Num):Z204121653102218116204120484120214531010112164403265528453
    result2:153
    z:7
    /*******************************/
    code2:.S1576130942.P190101.LABS20191201.E191201.V191201.C7
    LAST:MA.156.M0.100009.96006005.S1576130942.P190101.LABS20191201.E191201.V191201.C7
  • 相关阅读:
    pycharm 测试执行成功,但却无法成功生成测试报告(使用HTMLTestRunner)的解决办法
    Pycharm安装package报错:AttributeError: module 'pip' has no attribute 'main'
    解决pycharm新建工程项目都需要重新安装库问题
    使用grep排除空行和注释行
    Django实战(一)-----用户登录与注册系统5(图片验证码)
    Django实战(一)-----用户登录与注册系统4(表单)
    Django实战(一)-----用户登录与注册系统3(前端页面、登录视图)
    Django实战(一)-----用户登录与注册系统2(数据模型、admin后台、路由视图)
    Django实战(一)-----用户登录与注册系统1(环境搭建)
    python-----virtualenv 虚拟环境
  • 原文地址:https://www.cnblogs.com/txw1958/p/12029104.html
Copyright © 2020-2023  润新知