The algorithm of ID 15-digit rise 18-bit

Source: Internet
Author: User

Now that the new ID card has risen from 15 to 18, many software may use the ID card input, the verification work, regarding the new ID card code seems very few articles, I found in 2000 the computer world an article, discovered that now the ID card verification code actually possibly is the English letter x ( In fact, this x is the Roman alphabet, meaning 10, why use the alphabet? Who knows. At least the number key on the phone will not be a new number. (now the method is to use * substitution, fortunately there are #*2 a key, I have done CTI, hehe), and then I wrote a Delphi process, posted to the "NetEase", but not suitable for the elderly over 100 years old.

I found a lot of netizens asking this question, so I tidied it up and posted it again, new bottles of old wine.

According to the standard GB 11643-1999〗 of the People's Republic of China, the citizen identification number is a characteristic combination code, which consists of 17 digit ontology code and a digital check code. The order is from left to right: six digit address code, eight digit birth date code, three digit sequential code and one digit parity code.

The address code represents the administrative division code of the county (city, Flag, district) of the resident residence of the encoding object. The date code represents the year, month, and day of the encoding object's birth, where the year is represented by a four-digit number, and the year, month, and day are not separated. The sequential code represents the sequence number of persons born in the same same address code, in the range of the area identified by the same one. The odd number of sequential codes is given to men, even to females. The check code is based on the front 17 digit code, according to the ISO 7064:1983.mod 11-2 check code to calculate the inspection code. The following example illustrates the calculation method.

The 15-digit ID Code First expands the year of birth to 4 digits, simply by adding a 19, but this is not used for people born in 1900 (the birthday is not much)

A male citizen ID number is 34052419800101001, first calculated according to the formula ⑴:

∑ (AIXWI) (mod 11) ..... ...... ...... ...... ...... ...... ...... (1)

Formula (1):
I----the number of digits from the to left including the check code;
AI----represents the number character value on position I;
The Wi----shows the weighting factor in position I, whose numerical value is calculated according to the formula wi=2 (n-1) (mod 11).

I 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

Ai 3 4 0 5 2 4 1 9 8 0 0 1 0 1 0 0 1 A1

Wi. 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 1

AIXWI 0 2 9/0 0 9 0 5 0 0 2 A1

Calculated according to the formula (1):

∑ (AIXWI) = (21+36+0+25+16+16+2+9+48++0+0+9+0+5+0+0+2) = 189

189÷11 = 17 + 2/11

∑ (AIXWI) (mod 11) = 2

Then, based on the results of the calculation, the corresponding checksum code is detected from the table below, where x indicates that the result is 10:

∑ (AIXWI) (mod 11) 0 1 2 3 4 5 6 7 8 9 10
Parity code character value AI 1 0 X 9 8 7 6 5 4 3 2
According to the table above, the verification code with the result of 2 is found to be that the citizenship number of the person should be 34052419800101001X.


Here's my Delphi function.

function Idfun (id:string): string;
Const
W:array [1..18] of the integer = (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);
A:array [0..10] of char = (' 1 ', ' 0 ', ' x ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 ');
Var
I, J, S:integer;
newid:string;
Begin
If Length (ID) <> Then
Result:= '
ELSE begin
Newid:= ID;
Insert (' ', NewID, 7);
s:= 0;
Try
For i:=1 TODO begin
J:= Strtoint (Newid[i]) * W[i];
s:= S + j;
End
Except
Result:= ';
Exit
End
s:= S MoD 11;
result:= NewID + a[s];
End
End

I used these algorithms in php,c++ and JavaScript to use in Web sites and other programs.
I always feel that the algorithm is much better than the source code, with the above algorithm, I believe this code is not difficult to become other languages.

Please give me mail:pazee@21cn.com if you have any questions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.