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;

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;

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.

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:

Get the sum's unit digit

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);

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.

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:

Compute the sum of the non-check digits (67).

Multiply by 9 (603).

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.

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