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.