C# 算法 luhnNext(联通物联网卡ICCID号,默认最少19位,总长度20位)
public static int luhnSum(string InVal)
{
int evenSum;
int oddSum;
bool isEven;
bool IsOdd = true;
evenSum = 0;
oddSum = 0;
int strLen;
strLen = InVal.Length;
int i;
for (i = strLen; i >= 1; i--)
{
int digit;
digit = int.Parse(InVal.Substring(i - 1, 1));
if (IsOdd)
{
oddSum = oddSum + digit;
IsOdd = false;
}
else
{
digit = digit * 2;
if (digit > 9)
{
digit = digit - 9;
}
evenSum = evenSum + digit;
IsOdd = true;
}
}
int luhnSum = (oddSum + evenSum);
return luhnSum;
}
public static int luhnNext(string InVal)
{
int luhnNext;
int rst;
rst = luhnSum(InVal + 0) % 10;
if (rst == 0)
luhnNext = 0;
else
luhnNext = 10 - rst;
return luhnNext;
}
VB 的算法 luhnNext(联通物联网卡ICCID号,默认最少19位,总长度20位)
Function luhnSum(InVal As String) As Integer
Dim evenSum As Integer
Dim oddSum As Integer
Dim isEven As Boolean
IsOdd = True
evenSum = 0
oddSum = 0
Dim strLen As Integer
strLen = Len(InVal)
Dim i As Integer
For i = strLen To 1 Step -1
Dim digit As Integer
digit = CInt(Mid(InVal, i, 1))
If (IsOdd) Then
oddSum = oddSum + digit
IsOdd = False
Else
digit = digit * 2
If (digit > 9) Then
digit = digit - 9
End If
evenSum = evenSum + digit
IsOdd = True
End If
Next i
luhnSum = (oddSum + evenSum)
End Function
Function luhnCheck(InVal As String)
luhnCheck = (luhnSum(InVal) Mod 10) = 0
End Function
Function luhnNext(InVal As String)
Dim rst
rst = luhnSum(InVal & 0) Mod 10
If (rst = 0) Then
luhnNext = 0
Else
luhnNext = 10 - rst
End If
End Function