谁给一个可以正确的将15位身份证号码转为18位的C#语言写的函数
谁给一个可以正确的将15位身份证号码转为18位的C#语言写的函数啊?
谢谢!
[解决办法]
refer:http://www.xker.com/page/e2007/0116/10435.html
[解决办法]
string GetNewIDCard(string IDCard) {
int i = 0;
int S = 0;
string[] Wi = "7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1".Split(",");
string[] Wf = "1,0,X,9,8,7,6,5,4,3,2".Split(",");
if ((IDCard.Substring(6, 2) >= Now.AddYears(-14).Year.Substring(2, 2))) {
IDCard = (IDCard.Substring(0, 6) + ("18" + IDCard.Substring(6, 9)));
}
else {
IDCard = (IDCard.Substring(0, 6) + ("19" + IDCard.Substring(6, 9)));
}
for (i = 0; (i <= 16); i++) {
S = (S
+ (Wi[i] * IDCard.Substring(i, 1)));
}
return (IDCard + Wf[(S % 11)]);
}
[解决办法]
此方法亲测有效:
private static 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位并不可靠,算法可验证18位的正确性,但15位生成18位并不可靠。当然可能是没有身份证的算法说明。