1 /// 作者:林宏权 2 /// http://www.songzhuren.com/ 3 /// 4 using System; 5 using System.Collections.Generic; 6 using System.Linq; 7 using System.Text; 8 9 namespace IDCardCheck 10 { 11 class IDCard 12 { 13 14 //加权算法第一位到第十七位的系数: 15 int[] iW = new int[] { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; 16 17 //身份证效验码字符串,与加权算法对位对应. 18 string Lastcode = "10X98765432"; 19 20 /// <summary> 21 /// 获取身份证的第18位效验码. 22 /// </summary> 23 /// <param name="remainder"></param> 24 /// <returns>第18位效验码字字符串.</returns> 25 public string GetLastCode(int remainder) 26 { 27 //根据加权算法所获取的余数,返回对应的效验码. 28 return Lastcode[remainder].ToString(); 29 } 30 31 /// <summary> 32 /// 通过加权算法,获取效验码的余数. 33 /// </summary> 34 /// <param name="IDCardNumber"></param> 35 /// <returns></returns> 36 public int GetTheRemainder(string IDCardNumber) 37 { 38 int theSum = 0; 39 //通过加权算法,求出身份证前17位的加权和. 40 for (int position = 0; position < 17;position++ ) 41 { 42 theSum += int.Parse(IDCardNumber[position].ToString()) * iW[position]; 43 44 } 45 //加权和 除以 11 所得的余数为实验码对应余数. 46 return theSum % 11; 47 } 48 } 49 }
时间: 2024-10-29 19:06:21