`
bolutes
  • 浏览: 870631 次
文章分类
社区版块
存档分类
最新评论

.NET 15位身份证升级到18位的算法

 
阅读更多

相关链接:

  • VB函数

Public Shared Function per15To18(ByVal perIDSrc As String) As String

Dim [iS] As Integer = 0

'加权因子常数

Dim iW As Integer() = New Integer() {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}

'校验码常数

Dim LastCode As String = "10X98765432"

'新身份证号

Dim perIDNew As String

perIDNew = perIDSrc.Substring(0, 6)

perIDNew += "19"

perIDNew += perIDSrc.Substring(6, 9)

For i As Integer = 0 To 16

'进行加权求和

[iS] += Integer.Parse(perIDNew.Substring(i, 1)) * iW(i)

Next

'取模运算,得到模值

Dim iY As Integer = [iS] Mod 11

'LastCode中取得以模为索引号的值,加到身份证的最后一位,即为新身份证号。

perIDNew += LastCode.Substring(iY, 1)

Return perIDNew

End Function

  • C#函数

public 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);

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;

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics