The citizenship number is a characteristic combination code, which consists of a 17-digit ontology code and a digital parity code. Order from left to right is: six-digit address code, eight-digit birth date code, three-digit numeric sequence code and a digital parity code.
1. Address Code
Represents the code object resident Residence in the county (city, Flag, district) of the administrative area codes, in accordance with the provisions of GB/T 2260.
2. Birth date Code
Indicates the year, month, and day of birth of the encoding object, as specified in GB/t 7408. No separator between year, month, and day code.
For example: the date of birth of a person is October 26, 1966 and the date of birth is 19661026.
3. Sequential code
The sequence number assigned to a person born in the same year, the same month, and the same day in the range of areas identified by the same address code, the odd distribution of the order code to the male, and even thousands of to the female.
4. Check code
The calibration code adopts ISO 7064:1983,mod 11-2 check code system.
(1) The weighted summation formula of 17-digit body code
S = SUM (Ai * Wi), i = 0, ..., 16, first the sum of the first 17 digits
Ai: Indicates the ID number value of position I
Wi: A weighted factor representing position I
Wi:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4-2
(2) Computational model
Y = mod (S, 11)
(3) The corresponding check code is obtained through the model
Y:0 1 2 3 4 5 6 7 8 9 10
Check code: 1 0 X 9 8 7 6 5 4 3 2
Here is the Java implementation code
/*
* Idcard.java Created on 2004-11-5 17:03:37
*
*/
Package Org.yz21.study.idcard;
/**
* @author violin 2004-11-5 17:03:37
* Copyright www.yz21.org 2003-2004
*/
public class Idcard {
WI =2 (n-1) (mod 11)
Final int[] wi = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1};
Verify digit
Final int[] vi = {1,0, ' X ', 9,8,7,6,5,4,3,2};
Private int[] ai = new int[18];
Public Idcard () {
}
Verify
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 (Getverify (Idcard)) {
return true;
}
return false;
}
Get verify
public string getverify (string eightcardid) {
int remaining = 0;
if (eightcardid.length () = = 18) {
Eightcardid = eightcardid.substring (0, 17);
}
if (eightcardid.length () = = 17) {
int sum = 0;
for (int i = 0; i < i++) {
String k = eightcardid.substring (i, i + 1);
Ai = Integer.parseint (k);
}
for (int i = 0; i < i++) {
sum = sum + WI * AI;
}
remaining = sum% 11;
}
return remaining = = 2? "X": string.valueof (vi[remaining]);
}
Update to 18
public string Uptoeighteen (string fifteencardid) {
String Eightcardid = fifteencardid.substring (0,6);
Eightcardid = Eightcardid + "19";
Eightcardid = Eightcardid + fifteencardid.substring (6,15);
Eightcardid = Eightcardid + getverify (eightcardid);
return eightcardid;
}
}
Test code:
The Unit test tool used is JUnit
/*
* Idcardtest.java Created on 2004-11-5 17:32:12
*
*/
Package Org.yz21.study.idcard;
Import Junit.framework.Test;
Import Junit.framework.TestCase;
Import Junit.framework.TestSuite;
/**
* @author violin 2004-11-5 17:32:12
* Copyright www.yz21.org 2003-2004
*/
public class Idcardtest extends TestCase {
Private String Idcard1 = "11010519491231002X";
Private String Idcard2 = "440524188001010014";
public void Testverify () {
Idcard Idcard = new Idcard ();
This.asserttrue (Idcard. Verify (Idcard1));
This.asserttrue (Idcard. Verify (IDCARD2));
}
public static Test Suite () {
return new TestSuite (Idcardtest.class);
}
public static void Main (string[] args) {
Junit.textui.TestRunner.run (Suite ());
}
}