用C#實現由15位社會安全號碼升級到18位的演算法!

來源:互聯網
上載者:User
 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[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
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   for( int 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    Helper methods#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}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.