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

 1 None.gif using  System;
 2 None.gif using  System.Collections;
 3 None.gif
 4 None.gif public   class  MyClass
 5 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 6InBlock.gif    public static void Main()
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 8InBlock.gif        Console.WriteLine(per15To18("429005811009091"));
 9InBlock.gif        RL();
10ExpandedSubBlockEnd.gif    }

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

52InBlock.gif    
53InBlock.gif    private static void RL()
54ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
55InBlock.gif        Console.ReadLine();    
56ExpandedSubBlockEnd.gif    }

57InBlock.gif    
58InBlock.gif    private static void Break() 
59ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
60InBlock.gif        System.Diagnostics.Debugger.Break();
61ExpandedSubBlockEnd.gif    }

62InBlock.gif
63ExpandedSubBlockEnd.gif    #endregion

64ExpandedBlockEnd.gif}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值