C # code for converting the 15-digit to 18-digit ID card number of the Applet

Source: Internet
Author: User

Now we use all 18-digit ID card numbers, and the previous 15-digit ID card numbers. How can we convert a 15-digit ID card number to an 18-digit ID card number?

1. First, after 6th digits of the 15-digit ID card, "19" is changed to 17 digits, that is, the Year of birth plus 19 digits. For example, the original 92 years is changed to 1992 characters.

2. The calculation of the last digit is as follows:

1) Multiply each digit of the 17-digit ID card number obtained by 1 by the corresponding coefficient. The 17-digit coefficient is {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}

2) add (1) the number 17 and sum it to temp.

3) divide temp by 11 to get the remainder, temp % = 11;

4) use the result after the remainder as the subscript of the ending number array endNum [] to find the value endNum [temp], then add endNum [temp] to the 17-digit ID card and change it to 18-digit,

EndNum [] = {'1', '0', 'x', '9', '8', '7', '6', '5 ', '4', '3', '2 '};

Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Threading. Tasks;

Namespace IDCard
{
Class Program
{
/// <Summary>
/// Main Function
/// </Summary>
/// <Param name = "args"> </param>
Static void Main (string [] args)
{
Console. Write ("enter a 15-digit ID card number :");
String OldID = Console. ReadLine (); // enter the 15 ID card number
String newId = IDCardTranslation (OldID); // new ID card number
Console. WriteLine ("18-bit new ID card number: {0}", newId); // output
Console. ReadLine ();
}
/// <Summary>
/// Transfer the 15-digit ID card number to the 18-digit ID card number
/// </Summary>
/// <Param name = "oldCard"> 15-digit ID card number </param>
/// <Returns> 18-digit ID card number </returns>
Private static string IDCardTranslation (string oldCard)
{
Int [] coefficient = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; // coefficient of each digit of the 17-digit ID card number
Char [] endNum = {'1', '0', 'x', '9', '8', '7', '6', '5 ', '4', '3', '2'}; // The Last array of ID cards
If (oldCard. Length = 15) // determines if it is 15 characters
{
String newID = oldCard. Insert (6, "19"); // Add 19 to the first digit of the 15-digit ID card number and change it to 17
Int s = 0;
For (int I = 0; I <17; I ++) // each of the 17-digit numbers is multiplied by the corresponding coefficient.
{
Int temp = Convert. ToInt32 (newID [I]) * coefficient [I];
S + = temp; // sum after Multiplication Coefficient
}
S % = 11; // divide by 11 to obtain the remainder
NewID + = endNum [s]. ToString (); // character of the 17-digit number plus the subscript represented by the remainder in the ending number array endNum
Return newID; // return the new ID number.
}
Else
{
Return "";
}
}
}
}

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.