码迷,mamicode.com
首页 > 移动开发 > 详细

联通物联网卡ICCID号校验位算法

时间:2018-09-13 16:27:38      阅读:2104      评论:0      收藏:0      [点我收藏+]

标签:sum   UNC   return   public   step   默认   ext   boolean   fun   

 

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

 

联通物联网卡ICCID号校验位算法

标签:sum   UNC   return   public   step   默认   ext   boolean   fun   

原文地址:https://www.cnblogs.com/zhangjunwei87/p/9640603.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!