ID card number encoding principle: Number Structure
A citizenship number is a combination of features and consists of a 17-digit ontology code and a verification code. The six-digit address code, eight-digit birth date code, three-digit sequence code, and one-digit verification code are arranged from left to right.
Verification code calculation
(1) 17-digit ontology weighted sum formula
S = sum (ai * WI), I = 0,..., 16, first to the front
17-digit weighted sum
AI: indicates
ID card number on I
Wi: indicates
I-position Weighting Factor
Wi: 7 9 10 5 8
4 2 1 6 3
7 9 10 5 8
4 2
S: = (4*7 + 5*9 + 2*10 + 1*5 + 2*8 + 4*4 + 1*2 + 9*1 + 8*6 + 3*3 + 0*7 + 7*9 + 0*10 + 3*5 + 0*8 + 3*4 + 9*2) =
306;
(2) Computing Model
Y = Mod (S, 11)
Y = 306% 11 = 9;
(3) obtain the corresponding verification code through the modulo
Y: 0 1 2 3 4 5 6
7 8 9 10
Verification Code: 1 0 x
9 8 7 6 5
4 3 2
Java code implementation:
Class verifyidcard {// Wi = 2 (n-1) (mod 11); weighting factor final int [] Wi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1}; // check code final int [] Vi = {1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2}; private int [] Ai = new int [18]; Public verifyidcard () {}// the verification code public Boolean verify (string idcard) {If (idcard. length () = 15) {idcard = uptoeighteen (idcard);} If (idcard. length ()! = 18) {return false;} string verify = idcard. substring (17, 18); If (verify. equals (this. getverify (idcard) {return true;} return false;} // 15-bit Public String uptoeighteen (string fifteen) {stringbuffer eighteen = new stringbuffer (fifteen ); eighteen = eighteen. insert (6, "19"); Return eighteen. tostring () ;}// calculate the last checksum value Public String getverify (string eighteen) {int remain = 0; If (eighteen. Length () = 18) {eighteen = eighteen. substring (0, 17);} If (eighteen. length () = 17) {int sum = 0; For (INT I = 0; I <17; I ++) {string K = eighteen. substring (I, I + 1); AI [I] = integer. valueof (k) ;}for (INT I = 0; I <17; I ++) {sum + = wi [I] * Ai [I];} remain = sum % 11;} return remain = 2? "X": String. valueof (VI [remain]);}