延续上一篇,此则文为java版本校验企业中征码.
校验规则在上一篇中已经提过.所以话不多说,直接上代码.
//校验中征码public static boolean checkZZM(String value){//先进行正则匹配String reg = "^[A-Z0-9]{3}[0-9]{13}$";if(!value.matches(reg)){return false;}//取出校验位String code = value.substring(14,16);//前14位转化为char数组char[] idCode = value.substring(0,14).toCharArray();//加权因子int[] weight_factor = new int[]{1,3,5,7,11,2,13,1,1,17,19,97,23,29};int len = idCode.length;int num = 0;int temp = 0;//循环取和for(int i = 0; i<len; i++){//字母转数字if(idCode[i]>='A' && idCode[i]<='Z'){temp = (int) idCode[i]-55;}else {temp = (int)idCode[i]-48;}//求和num = num + temp * weight_factor[i];}//取余+1int resisue = num % 97 + 1;if(Integer.parseInt(code)-resisue == 0){return true;}return false;}
如有规则不清楚可参考上一篇js校验企业中征码. 原理和js校验一样,只是多做一些变量类型的转换.如果错误或者可以优化的地方,烦请大佬多多指教,谢谢!!!