Calculate the last digit of the ID card

Source: Internet
Author: User

I encountered a problem today. I calculated 18th digits based on the first 17 digits of my ID card. This is clear.Algorithm, As shown below:
SIGMA (AI x WI) (mod 11 ).......................................... (1)
Formula (1:
I ---- indicates the serial number of the character including the verification code from right to left;
AI ---- indicates the number character value at position I;
Wi --- indicates the weighted value of each digit.
The left-to-right WI values are as follows:
Wi 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 1
Then, based on the formula (1), find the corresponding verification code from the following table. X indicates that the calculation result is 10:
Formula Calculation Result 0 1 2 3 4 5 6 7 8 9 10
Verification Code character value 1 0x9 8 7 6 5 4 3 2
The implementation of this algorithm is quite simple. It is easier to use the container in X ++:

Static Void Getlastnumber (STR stridentity)
{
Container factor = [ 7 , 9 , 10 , 5 , 8 , 4 , 2 , 1 , 6 , 3 , 7 , 9 , 10 , 5 , 8 , 4 , 2 ];
Container C = [ 1 , 0 , ' X ' , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 ];
Int I;
Int J;
;

If (Strlen (stridentity) ! = 17 )
Throw Info ( " Enter a 17-digit valid number! " );

For (I = 1 ; I <= 17 ; I ++ )
J + = Str2int (substr (stridentity, I, 1 )) * Conpeek (factor, I );

J = J mod 11 ;

Box: Info (strfmt ( " The last digit of your ID card is: % 1 " , J = 2 ? Conpeek (C, J + 1 ): Int2str (conpeek (C, J + 1 ))));
}

Of course, this does not check whether each character is a valid number.

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.