-3

Can anyone give me the code in C#... for getting the Verification Digit with Mod11?

Thanks.

public class Mod11 
{
    public static string AddCheckDigit(string number); 
}

Example:

Mod11.AddCheckDigit("036532");

Result: 0365327

Fraga
  • 1,361
  • 2
  • 15
  • 47
  • 4
    What have you tried? This is not a site where we solve your problem. This is a site where we help you solve your own problem, and hopefully improve your understanding along the way. Questions that say "give me the code" are especially likely to be kicked off the site. – Ben Voigt Aug 10 '11 at 05:00
  • Oh, i didnt knew that!.. i will erase it – Fraga Aug 10 '11 at 05:03
  • Also, your example is especially poor because it uses octal notation, and check digits operate on decimal digits. Or didn't you realize that an `int` argument doesn't preserve formatting, only the value? – Ben Voigt Aug 10 '11 at 05:04
  • I would suggest starting with the specification for the algorithm and see if you can't translate it into code: http://www.pgrocer.net/Cis51/mod11.html. Cheers :) – mellamokb Aug 10 '11 at 05:04
  • @Fraga: It would be ok to have the question, if you can provide your own answer below. – mellamokb Aug 10 '11 at 05:05

1 Answers1

3

The code is here:

public class Mod11
{
    public static string AddCheckDigit(string number)
    {
        int Sum = 0;
        for (int i = number.Length - 1, Multiplier = 2; i >= 0; i--)
        {
            Sum += (int)char.GetNumericValue(number[i]) * Multiplier;

            if (++Multiplier == 8) Multiplier = 2;
        }
        string Validator = (11 - (Sum % 11)).ToString();

        if (Validator == "11") Validator = "0";
        else if (Validator == "10") Validator = "X";

        return number + Validator;
    }
}

I hope it help some one.

PROBLEMS: If the remainder from the division is 0 or 1, then the subtraction will yield a two digit number of either 10 or 11. This won't work, so if the check digit is 10, then X is frequently used as the check digit and if the check digit is 11 then 0 is used as the check digit. If X is used, then the field for the check digit has to be defined as character (PIC X) or there will be a numeric problem.

Fraga
  • 1,361
  • 2
  • 15
  • 47