gpt4 book ai didi

.net - 检查有效的 IMEI

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:45:10 25 4
gpt4 key购买 nike

有人知道如何检查有效的 IMEI 吗?

我在这个页面上找到了一个函数来检查:http://www.dotnetfunda.com/articles/article597-imeivalidator-in-vbnet-.aspx

但对于有效的 IMEI(例如 352972024585360),它返回 false。我可以在此页面上在线验证它们:http://www.numberingplans.com/?page=analysis&sub=imeinr

检查给定 IMEI 是否有效的正确方法(在 VB.Net 中)是什么?

附言:上面页面中的这个函数在某些方面一定是不正确的:

Public Shared Function isImeiValid(ByVal IMEI As String) As Boolean
Dim cnt As Integer = 0
Dim nw As String = String.Empty
Try
For Each c As Char In IMEI
cnt += 1
If cnt Mod 2 <> 0 Then
nw += c
Else
Dim d As Integer = Integer.Parse(c) * 2 ' Every Second Digit has to be Doubled '
nw += d.ToString() ' Genegrated a new number with doubled digits '
End If
Next
Dim tot As Integer = 0
For Each ch As Char In nw.Remove(nw.Length - 1, 1)
tot += Integer.Parse(ch) ' Adding all digits together '
Next
Dim chDigit As Integer = 10 - (tot Mod 10) ' Finding the Check Digit my Finding the Remainder of the sum and subtracting it from 10 '
If chDigit = Integer.Parse(IMEI(IMEI.Length - 1)) Then ' Checking the Check Digit with the last digit of the Given IMEI code '
Return True
Else
Return False
End If
Catch ex As Exception
Return False
End Try
End Function

编辑:这是我的工作“checkIMEI”-功能:

Public Shared Function checkIMEI(ByRef IMEI As String) As Boolean
Const allowed As String = "0123456789"

Dim cleanNumber As New System.Text.StringBuilder
For i As Int32 = 0 To IMEI.Length - 1
If (allowed.IndexOf(IMEI.Substring(i, 1)) >= 0) Then
cleanNumber.Append(IMEI.Substring(i, 1))
End If
Next

If cleanNumber.Length <> 15 Then
Return False
Else
IMEI = cleanNumber.ToString
End If

For i As Int32 = cleanNumber.Length + 1 To 16
cleanNumber.Insert(0, "0")
Next

Dim multiplier As Int32, digit As Int32, sum As Int32, total As Int32 = 0
Dim number As String = cleanNumber.ToString()

For i As Int32 = 1 To 16
multiplier = 1 + (i Mod 2)
digit = Int32.Parse(number.Substring(i - 1, 1))
sum = digit * multiplier
If (sum > 9) Then
sum -= 9
End If
total += sum
Next

Return (total Mod 10 = 0)
End Function

最佳答案

IMEI 号码使用 Luhn 进行验证算法。链接页面有多种语言的实现。 This post还有一些关于如何解决 Luhn 算法的实现和一般方法。

关于.net - 检查有效的 IMEI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2516579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com