ASP.NETは15ビットのID番号を18ビットに変換

2873 ワード


//*********************************************************************************
    // 15       18  ,        4 ,     15     6    
    //    19,    17           ,      17         
    //   ,     ,            。
    //       
    //(1)17            
    //S=Sum(Ai*Wi),i=0,...,16,   17       
    //Ai:   i            
    //Wi:   i        
    //Wi;7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
    //
    //(2)   
    //Y=mod(S,11)
    //(3)           
    //Y:0 1 2 3 4 5 6 7 8 9 10
    //   :1 0 X 9 8 7 6 5 4 3 2
    //*****************************************************************************
    /// <summary>
    ///  15          18       
    /// </summary>
    /// <param name="idCard">     </param>
    /// <returns>  18      </returns>
    public static string Convert15to18 (string idCard)
    {
        string code = idCard.Trim();//       
        if (code.Length==15)//   15    
        {            
            char[] strJY ={ '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
            int[] intJQ ={ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
            string strTemp;
            int intTemp = 0;
            strTemp = code.Substring(0, 6) + "19" + code.Substring(6);
            for (int i = 0; i <= strTemp.Length - 1; i++)
            {
                intTemp = intTemp + int.Parse(strTemp.Substring(i, 1)) * intJQ[i];
            }
            intTemp = intTemp % 11;
            return strTemp + strJY[intTemp];
        }
        else
        {
            if (code.Length == 18)//   18     
            {
                return code;
            }
            return string.Empty;//     15    18     
        }       
    }
    /// <summary>
    ///        
    /// </summary>
    /// <param name="idCard">     </param>
    /// <returns>       </returns>
    public static string GetBirth(string idCard)
    {
        string code = Convert15to18(idCard);//       
        if (code != string.Empty)
        {
            string year = code.Substring(6, 4);//           
            string month = code.Substring(10, 2);//           
            string day = code.Substring(12, 2);//           
            return year + " " + month + " " + day + " ";
        }
        else
        {
            return string.Empty;
        }
    }