Home > C# > Generating Luhn Check Digit

Generating Luhn Check Digit

Howdy,

Today I made a small app just to generate the Luhn Check digit for a 19 digit number. Here’s the
Luhn Algorithms Informal Explanation
for you, if you want to take a look at its algorithmic discussion.

Luhn Check Digit Generator

Luhn Check Digit Generator

Luhn Check Application

 

Implementation:-

private void btnGenerate_Click(object sender, EventArgs e)
{
if (txtNumber.Text.Length == 19)
{
int total = 0;
for (int i = txtNumber.Text.Length – 1; i >= 0; i–)
{
if (i % 2 == 0)
{
total += Convert.ToInt32(txtNumber.Text[i].ToString());
}
else
{
total += BreakNumberAndAdd(multiplyByTwo(txtNumber.Text[i]));
}
}
if (total % 10 == 0)
{
lblLuhnCheckDigit.Text = “Luhn Check Digit: 0″;
}
else
{

string s_LuhnCheckDigit = GetLuhnCheckDigitFromTotal(total).ToString();

lblLuhnCheckDigit.Text = “Luhn Check Digit: ” + s_LuhnCheckDigit;

}

}

}
///
/// Get the Luhn Check number of the number specified by the total.
///
///
The number from which Luhn Check digit is to be created. ///
private int GetLuhnCheckDigitFromTotal(int total)
{
return Convert.ToInt32(10 – (total % 10));
}

private void btnExit_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show(this, “Are you sure you want to close this application?”,
“Exit Confirmation”, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if(dr == DialogResult.Yes)
Application.Exit();
}
///
/// Multiplies the given number by 2
///
///
///
private int multiplyByTwo(char digit)
{
return (Int32.Parse(digit.ToString()) * 2);
}
///
/// Breaks the 2 digit number and adds the individual digits like 11 to 1+1=2. One digit number would remain as it is.
///
///
///
private int BreakNumberAndAdd(int number)
{
string s_num = number.ToString();
return (s_num.Length == 2) ? Int32.Parse(s_num[0].ToString()) + Int32.Parse(s_num[1].ToString()) : number;
}

 

Download the code

  1. Amjad Khan
    August 28, 2008 at 6:47 am | #1

    hi Imran,

    Just wanted to tell you that this article really solved my problem. Thanks

    Keep up the good work

  2. Joe Mixon
    February 23, 2009 at 4:16 pm | #2

    Good Morning,

    This is great but does not quite solve my issue. Do you know of or could this be modified to produce a self check digit for any number length. I know it would be necessary to have a entry field telling the program how many digits are in the calculation. In addition could it be batched and run through the program.

    Thanks

    • Imran Akram
      February 24, 2009 at 12:52 pm | #3

      Hi Joe, thanks for visiting my blog.

      well, I think its pretty simple to modify this code for any number length. I havent been able to run and test the below mentioned code on my end but it was in a similar form as this for an 18 digit number. The point is to double every second digit. So having that in mind. I think this should work well enough. Hope that helps. Do let me know if you needed some changes to this or if this worked all good for you. Thanks

      public static int GetLuhnCheckDigit(string s_Number)
      {
      decimal dec = 0;
      if (!Decimal.TryParse(s_Number, out dec))
      {
      throw new InvalidCastException(“Unable to process the number passed.”);
      }

      int total = 0;
      char digit = ‘ ‘;
      for (int i = s_Number.Length – 1; i >= 0; i–)
      {
      digit = s_Number[i];
      if (i % 2 == 0)
      {

      total += Convert.ToInt32(digit.ToString());
      }
      else
      {

      total += BreakNumberAndAdd(Convert.ToInt32(digit.ToString()) * 2);
      }

      }

      return ((total % 10) == 0) ? 0 : GetLuhnCheckDigitFromTotal(total);

      }

      return ((total % 10) == 0) ? 0 : GetLuhnCheckDigitFromTotal(total);

      }
      ///
      /// Get the Luhn Check number of the number specified by the total.
      ///
      /// The number from which Luhn Check digit is to be created.
      ///
      private static int GetLuhnCheckDigitFromTotal(int total)
      {
      return Convert.ToInt32(10 – (total % 10));
      }
      ///
      /// Breaks the 2 digit number and adds the individual digits like 11 to 1+1=2. One digit number would remain as it is.
      ///
      ///
      ///
      private static int BreakNumberAndAdd(int number)
      {
      string s_num = number.ToString();
      return (s_num.Length == 2) ? Int32.Parse(s_num[0].ToString()) + Int32.Parse(s_num[1].ToString()) : number;
      }

      I think the above code should work for u. But please note that I havent been able to run the code on my end, I’ve just dry run it. So do use it with care.

  1. No trackbacks yet.