2014年3月12日水曜日

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

/// 
/// ルーンズ(モジュラス10/ウェイト2 計算
/// NW-7
/// 
/// /// 
public static string GetModulus10Weight2Runes(string Value)
{
    bool result = Regex.IsMatch(Value, @"^[0-9]+$");
    if (!result)
    {
        return null;
    }

    long checkDigit = 0;

    for (int i = 0; i < Value.Length; i++)
    {
        if (i % 2 == 0)
        {
            int x = int.Parse(Value.Substring(Value.Length - 1 - i, 1)) * 2;
            //10以上の場合、桁ごとに加算
            if (10 <= x) x = 1 + (x % 10);

            checkDigit += x;
        }
        else
        {
            checkDigit += int.Parse(Value.Substring(Value.Length - 1 - i, 1));
        }
    }

    checkDigit = 10 - (checkDigit % 10);

    checkDigit = checkDigit == 10 ? 0 : checkDigit;

    return checkDigit.ToString();
}

0 件のコメント:

コメントを投稿