2014年3月12日水曜日

C#で加重モジュラス11の計算

  1. /// <summary>  
  2. /// 加重モジュラス11 計算  
  3. /// NW-7  
  4. /// </summary>  
  5. /// <param name="Value">/// <returns></returns>  
  6. public static string GetWeightedModulus11(string Value)  
  7. {  
  8.     bool result = Regex.IsMatch(Value, @"^[0-9]+$");  
  9.     if (!result || Value.Length > 12)  
  10.     {  
  11.         return null;  
  12.     }  
  13.   
  14.     int[] weight1 = { 2, 6, 3, 5, 4, 8, 7, 10, 9, 5, 3, 6, };  
  15.     int[] weight2 = { 9, 5, 8, 6, 7, 3, 4, 10, 2, 6, 8, 5, };  
  16.   
  17.     try  
  18.     {  
  19.         int checkDigit = 0;  
  20.         for (int i = 0; i < 2; i++)  
  21.         {  
  22.             checkDigit = 0;  
  23.             for (int j = 0; j < Value.Length; j++)  
  24.             {  
  25.                 checkDigit += int.Parse(Value.Substring(Value.Length - 1 - j, 1)) * (i == 0 ? weight1[j] : weight2[j]);  
  26.             }  
  27.             checkDigit = checkDigit % 11;  
  28.   
  29.             if (checkDigit == 0) return "0";  
  30.             if (checkDigit != 1) break;  
  31.         }  
  32.   
  33.         return (11 - checkDigit).ToString();  
  34.     }  
  35.     catch  
  36.     { }  
  37.     return null;  
  38. }  

0 件のコメント:

コメントを投稿