# Luhn algorithm check digit .net checksum algorithm for multiple data type

Good Morning.

I try to mod a .net from so it will output Luhn algorithm check digit

it has been 3 days ,i still can't get it to work ,any ideas?

Diogo Rosa also try to help me .but i am new to .net .

in  --> Number

out  <-- Calculated Luhn algorithm check digit. if number out pot 999

public class CssLuhnAlgorithm: IssLuhnAlgorithm {
/// <summary>
/// Check if number is correct conform Luhn's algorithm laws.
/// </summary>
/// <param name="ssdigits">Number to be tested</param>
public void MssCheckLuhnAlgorithm(string ssdigits, out string sschecksum) {
// TODO: Write implementation for action
int j = 0;

bool bOdd = true;

int[] DELTAS = new int[] { 0, 1, 2, 3, 4, -4, -3, -2, -1, 0 };
int checksum = 0;
char[] chars = ssdigits.ToCharArray();

for (int i = chars.Length - 1; i > -1; i--)
{
j = ((int)chars[i]) - 48;

// If the character wasn't numeric, then just return false
if ((j < 0) || (j > 9)) checksum = 999;

checksum += j;

// This loses us the subtraction and mod operations on each iteration and switches to a bitwise/logic operator
if (bOdd) checksum += DELTAS[j];
bOdd = !bOdd;

}
sschecksum = checksum.ToString();
} // MssCheckLuhnAlgorithm

} // CssLuhnAlgorithm

} // OutSystems.NssLuhnAlgorithm

Hi Weixi,

I'm not familiar with Luhn's algorithm, but what do you mean by "I still can't get it to work"? Is it producing the wrong checksum?

EDIT: Also, why don't you use plain OutSystems instead of .NET?

yes it is producing the wrong checksum.

I don't know there is a way to use plain outsystem to get a check sum .

i working on a app that need input ICCID Number for Metropcs/T-Mobile .

input as   890126096318289651  <--- basic Account Info

output  check dig 9

http://www.simplycalc.com/luhn-calculate.php

i can't use any JavaScript ,it should run at server side for safety reason ,any ideas?

thank you so much for your help .

According to this, calculating the check-digit is as simple as:

1. Sum all digits
2. Multiply the result by 9
3. the check digit will be remainder of the division of that value by 10.

Implementing this algorithm is fairly simple to implement in OutSystems, following this simple pseudo-code:

```while (input > 0) {
sum = sum + (input % 10);
input = input / 10;
}
checkdigit = (sum * 9) % 10;```

where:

• input is the number you want to calculate the check digit for;
• sum is a temporary variable to hold intermediate calculations;
• checkdigit is the check digit you want as output;
• % represents the Mod function in OutSystems;
• / is the integer division (returning an integer/long integer).

Jorge,

I think that description also calls for doubling every second digit? Anyway, it's still fairly simple. I'll see if I can make a reference implementation using OutSystems proper, instead of an extension.

Hi Kilian,

No, the doubling every other digit is for validating a number including the check digit. What Weixi You was requesting was how to calculate the check digit.

The alternative to the algorithm I described is, instead of 2./3. above, these:

1. Get the sum's unit digit
2. Subtract that digit from 10.

which would be this alternative pseudo-code:

```while (input > 0) {
sum = sum + (input % 10);
input = input / 10;
}
checkdigit = 10 - (sum % 10);```

Jorge,

I'm sorry, but I disagree. Doubling really seems needed also when calculating the check digit. Also, 10 - (sum % 10) can't be right, since that could produce 10.

Hi Kilian,

I looked into doc sent by Jorge (Wikipedia), and from that description, to generate the chrck digit there is no need to double the second values...s

This is what is stated there:

The check digit (x) is obtained by computing the sum of the non-check digits then computing 9 times that value modulo 10 (in equation form, ((67 × 9) mod 10)). In algorithm form:

1. Compute the sum of the non-check digits (67).
2. Multiply by 9 (603).
3. The units digit (3) is the check digit. Thus, x=3.

Then I disagree with you both, sorry guys :) In the meantime, I wrote a Luhn checkdigit OutSystems version, which does use the doubling, and calculates the correct checkdigit according to some online tools. I'll include it here in a while.

Would be the case where the Wikipedia is wrong????? ????

No, would be the case that Wikipedia is not very clear, but correct after all :).

Kilian Hekhuis wrote:

Jorge,

I'm sorry, but I disagree. Doubling really seems needed also when calculating the check digit. Also, 10 - (sum % 10) can't be right, since that could produce 10.

You're right Kilian, Wikipedia is correct, the confusion is they use the term the sum of the non-check digits to mean the "special" sum (including doubled digits reduced to single digit). My bad.

As for the `10 - (sum % 10)`... it's mostly correct ;) I forgot the corner case:

`In case the sum of digits ends in 0 then 0 is the check digit.`

Kilian Hekhuis wrote:

No, would be the case that Wikipedia is not very clear, but correct after all :).

Uff! I was already seeing my world falling apart!!!

Solution

Ok, here's a working example. You can test it here.

Solution