C # implementation 18 identity format verification algorithm

zhaozj2021-02-17  78

The 18 identity card standards made a clear provision in GB11643-1999 "Citizen ID" implemented on July 1, 1999. GB11643-1999 "Citizenship Number" is the revision of the "Social Security Number", which points to the original standard name "Social Security Number" is renamed "Citizenship Number", and the GB11643-1999 "Citizen ID Number" Instead of GB11643-1989 from the date of implementation. GB11643-1999 The main contents of "Citizenship Number" are as follows: 1. Range The standard specifies the coding object, number of citizen numbers, and the structure and performance form of the number, enabling each encoding object to get a unique, unchanged legal number. Second, the encoding object of coding object citizenship number is a citizen with nationality of the People's Republic of China. Third, the structure and representation of the number 1, the number of the citizenship number of the number is the characteristic group code, consisting of the seventeenth digital body code and a check code. Arrange the order from left to right as: six digit address code, eight digit born date code, three digital sequence code and one digital check code. 2, address code represents administrative division code of the county (city, flag, district) of the coded object, and is implemented in accordance with the provisions of GB / T2260. 3. The birth date indicates the year, month, day, and according to the provisions of the GB / T7408, the year, month, and Japanese code shall not be separated from the date, month, month. 4, the sequence code indicates the sequence number of the same year, the same month, the same day, the odd number of the sequence code, the odd number of sequence codes, the odd number of sequence codes to women, even if the sequence code is assigned to the male. 5, check code (1) seventeen digital body code weighted summation and formula S = SUM (AI * Wi), i = 0, ..., 16, first on the top 17 digits and AI: Identity card number in the first location WI: Represents the weighting factor Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2

(2) Calculation model Y = mod (s, 11)

(3) Get the corresponding check code 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, for example: Beijing Chaoyang District: 11010519491231002X Shantou City, Guangdong Province: 440524188001010014

Below is the code implemented by C #

using System; namespace Arithmetic {public class VerifyIDCard {public VerifyIDCard (string pszSr) {if (pszSr.Length = 18!) {throw new System.ArgumentException ( "Éí · ÝÖ¤ºÅÂë²» ¶Ô ");} this.identidyCard = Pszsr.tochararray ();} public char DOVERIFY () {INT IS = 0; INT i; for (i = 0; i <17; i ) {is = (int) (IdentiDycard [i] - '0') * IW [i];} int = IS% 11; return Szvercode [iY];

} Private char [] identidycard; private static char [] szvercode = {'1', '0', 'x', '9', '8', '7', '6', '5', '4' , '3', '2'}; private int [] IW = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };}}

Below is the test code:

The unit test tool used is NUNIT (details for details)

namespace Arithmetic {using System; using NUnit.Framework; [TestFixture] public class VerfityIDCardTest {public VerfityIDCardTest () {} [Test] [ExpectedException (typeof (ArgumentException))] public void testVerfityIDCardConstructor () {VerifyIDCard IDcard = new VerifyIDCard ( "4405241880010100125 ");} [Test] public void testdover () {char s; verifyidcard vfid = new Verifyidcard (" 11010519491231002x "); s = vfid.doverify (); assert.arequal (s, 'x'); verifyidcard vfid1 = new VerifyidCard ("44052418001010014); s = vfid1.doverify (); assert.arequal (s, '4');}}}}}} full program download VerifyidCard.ra: development environment (Windows 2000 Server .NET Framework1.1 SharpDevelop0 .98 NUNIT2.1)

转载请注明原文地址:https://www.9cbs.com/read-31270.html

New Post(0)