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
ai×Wi 21 36 0 25 16 16 2 9 48 0 0 9 0 5 0 0 2 a1
根据公式(1)进行计算:
∑(ai×Wi) =(21+36+0+25+16+16+2+9+48++0+0+9+0+5+0+0+2) = 189
189 ÷ 11 = 17 + 2/11
∑(ai×Wi)(mod 11) = 2
然后根据计算的结果,从下面的表中查出相应的校验码,其中X表示计算结果为10:
∑(ai×WI)(mod 11) 0 1 2 3 4 5 6 7 8 9 10
校验码字符值ai 1 0 X 9 8 7 6 5 4 3 2
根据上表,查出计算结果为2的校验码为所以该人员的公民身份号码应该为 34052419800101001X。
C#代码:
private string per15To18(string perIDSrc)
{
int iS = 0;
//加权因子常数
int[] iW=new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
//校验码常数
string LastCode="10X98765432";
//新号
string perIDNew;
perIDNew=perIDSrc.Substring(0,6);
//填在第6位及第7位上填上‘1’,‘9’两个数字
perIDNew += "19";
perIDNew += perIDSrc.Substring(6,9);
//进行加权求和
for( int i=0; i<17; i++)
{
iS += int.Parse(perIDNew.Substring(i,1)) * iW[i];
}
//取模运算,得到模值
int iY = iS%11;
//从LastCode中取得以模为索引号的值,加到的最后一位,即为新号。
perIDNew += LastCode.Substring(iY,1);
return perIDNew;
}
--------------------------------------------------------- -----------------------------------------------------------------------------------------
15位号码转18位算法
classCIDCardNumber
{
//最后的18位号码
TCHARszIdCard18[MAX_PATH];
//经过初次变换后的15位号码
charai[18];
public:
/*
功能:15位号码转换为18位
参数:chCodeNumber原15位号码
*/
char*IDCard15To18(constchar*constchCodeNumber)
{
//初次变换,把出生年由2位变为4位
//最后一位a代表未知。
ai[0]=chCodeNumber[0];
ai[1]=chCodeNumber[1];
ai[2]=chCodeNumber[2];
ai[3]=chCodeNumber[3];
ai[4]=chCodeNumber[4];
ai[5]=chCodeNumber[5];
ai[6]='1';
ai[7]='9';
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-27736-2.html
美国现在巴不得我们打