[转]用C#实现由15位身份证号升级到18位的算法

本文介绍了一种将15位旧版身份证号码转换为18位新版号码的算法实现。通过加权求和和取模运算确定校验码,完成新身份证号码的生成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1using System;
 2using System.Collections;
 3
 4public class MyClass
 5{
 6    public static void Main()
 7    {
 8        Console.WriteLine(per15To18("429005811009091"));
 9        RL();
10    }

11    
12    public static string per15To18(string perIDSrc) 
13  
14   int iS = 0
15 
16   //加权因子常数 
17   int[] iW=new int[]{7910584216379105842};
18   //校验码常数 
19   string LastCode="10X98765432"
20   //新身份证号 
21   string perIDNew; 
22 
23   perIDNew=perIDSrc.Substring(0,6);
24   //填在第6位及第7位上填上‘1’,‘9’两个数字 
25   perIDNew += "19"
26 
27   perIDNew += perIDSrc.Substring(6,9); 
28 
29   //进行加权求和 
30   forint i=0; i<17; i++
31   
32    iS += int.Parse(perIDNew.Substring(i,1)) * iW[i]; 
33   }
 
34     
35   //取模运算,得到模值 
36   int iY = iS%11;
37   //从LastCode中取得以模为索引号的值,加到身份证的最后一位,即为新身份证号。 
38   perIDNew += LastCode.Substring(iY,1); 
39
40   return perIDNew;
41  }
 
42
43
44
45    
46    #region Helper methods
47
48    private static void WL(object text, params object[] args)
49    {
50        Console.WriteLine(text.ToString(), args);    
51    }

52    
53    private static void RL()
54    {
55        Console.ReadLine();    
56    }

57    
58    private static void Break() 
59    {
60        System.Diagnostics.Debugger.Break();
61    }

62
63    #endregion

64}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值