香港身份证规则

 1  public static void main(String[] args) {
 2         String str = "C668668(9)";
 3         test te = new test();
 4         boolean aa = te.isHKIdentiy(str);
 5 
 6         System.out.println(aa);
 7 
 8     }
 9 
10     public boolean isHKIdentiy(String identity) {
11         if (identity == null){
12             return false;
13         }
14         if (identity.length() == 10) {
15 
16             String id1 = null;
17             String id6 = null;
18             String id8 = null;
19             String id9 = null;
20             String id10 = null;
21             id6 = identity.substring(1, 7);
22             id1 = identity.substring(0, 1);
23             id8 = identity.substring(7, 8);
24             id9 = identity.substring(8,9);
25             id10 = identity.substring(9, 10);
26             int aa0 = Integer.valueOf(id6);
27             int aa1 = Integer.valueOf(id1.charAt(0));
28             int aa2 = Integer.valueOf(identity.substring(1, 2));
29             int aa3 = Integer.valueOf(identity.substring(2, 3));
30             int aa4 = Integer.valueOf(identity.substring(3, 4));
31             int aa5 = Integer.valueOf(identity.substring(4, 5));
32             int aa6 = Integer.valueOf(identity.substring(5, 6));
33             int aa7 = Integer.valueOf(identity.substring(6, 7));
34             int aa9 = Integer.valueOf(id9.charAt(0));
35             if ((id8.equals("(")&& id10.equals(")") && (100000 <= aa0 && aa0 <= 999999) && (64 < aa1 && aa1 < 91) && (65 == aa9 || (48 <= aa9 && aa9 <= 57)))) {
36                 Integer sum = (aa1-64)* 8 + aa2 * 7 + aa3 * 6 + aa4 * 5 + aa5 * 4 + aa6 * 3 + aa7 * 2;
37                 Integer ys = Math.floorMod(sum, 11);
38                 String bb = String.valueOf(11 - ys);
39                 if ((ys == 1 && (id9.equals("A")) || (ys == 0 && (id9.equals("1"))) || ((1 < ys && ys < 11) && (id9.equals(bb))))) {
40                     return true;
41 
42                 } else return false;
43 
44             } else
45                 return false;
46         } else
47             return false;
48     }

香港:C668668(9)

括弧内的为校验码,用来校验前面的数字是否正确,可能为数字或者字母A
计算方法:

由前7位确定,首位字母改为数字代表,即A以1代表,B以2代表...Z以26代表,可以得到8个数字,之后第一个数字乘以8,第二个数字乘以7,依此类推,第七个数字乘以2

再将以上所有乘积相加,得到一个数,再将这个数除以11,得到余数。如果整除,校验码为0,如果余数为1,则校验码为A,如果余数为2~10,则用11减去这个余数,则为校验码。

原文地址:https://www.cnblogs.com/bai123/p/11539663.html