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

Advertisements

Author: Imran Akram

A .NET/SharePoint Consultant and a political enthusiast who believes in putting the feet of the elected and/or unelected officials to fire in order to get good governance.

5 thoughts on “Generating Luhn Check Digit”

  1. 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

    1. 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.

  2. Hi

    I think your code is not correct, well I generated a few number with check digits and verified them with online tools and none passed.

    I thinks it is on this line “if (i % 2 == 0)” it should be “if ((i+1) % 2 == 0)”, because you are not counting the check digit space that will be appended.

    Check it out, thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s