2014年3月12日水曜日

C#でルーンズ(モジュラス10/ウェイト2)の計算

  1. /// <summary>  
  2. /// ルーンズ(モジュラス10/ウェイト2 計算  
  3. /// NW-7  
  4. /// </summary>  
  5. /// <param name="Value">/// <returns></returns>  
  6. public static string GetModulus10Weight2Runes(string Value)  
  7. {  
  8.     bool result = Regex.IsMatch(Value, @"^[0-9]+$");  
  9.     if (!result)  
  10.     {  
  11.         return null;  
  12.     }  
  13.   
  14.     long checkDigit = 0;  
  15.   
  16.     for (int i = 0; i < Value.Length; i++)  
  17.     {  
  18.         if (i % 2 == 0)  
  19.         {  
  20.             int x = int.Parse(Value.Substring(Value.Length - 1 - i, 1)) * 2;  
  21.             //10以上の場合、桁ごとに加算  
  22.             if (10 <= x) x = 1 + (x % 10);  
  23.   
  24.             checkDigit += x;  
  25.         }  
  26.         else  
  27.         {  
  28.             checkDigit += int.Parse(Value.Substring(Value.Length - 1 - i, 1));  
  29.         }  
  30.     }  
  31.   
  32.     checkDigit = 10 - (checkDigit % 10);  
  33.   
  34.     checkDigit = checkDigit == 10 ? 0 : checkDigit;  
  35.   
  36.     return checkDigit.ToString();  
  37. }  

0 件のコメント:

コメントを投稿