Check for valid IMEI

筅森魡賤 提交于 2019-12-06 03:51:25

问题


Does anybody know how to check for a valid IMEI?

I have found a function to check on this page: http://www.dotnetfunda.com/articles/article597-imeivalidator-in-vbnet-.aspx

But it returns false for valid IMEI's (f.e. 352972024585360). I can validate them online on this page: http://www.numberingplans.com/?page=analysis&sub=imeinr

What is the correct way(in VB.Net) to check if a given IMEI is valid?

PS: This function from above page must be incorrect in some way:

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

EDIT: This is my working "checkIMEI"-Function:

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

回答1:


IMEI numbers are validated using the Luhn algorithm. The linked page has implementations in various languages. This post also has a few more implementations and a general methodology on how to go about solving the Luhn algorithm.




回答2:


IMEISV(IMEI software version) does not have Luhn algorithm check digit. Instead it has Software Version Number presented with two digits. Format of IMEI number has been evolving by years.

Wikipedia is the good source I think to see the changes in the format by years. If you look the new IMEI and IMEISV version format, you would see that Final Assembly Code(FAC) has removed and Type Allocation Code(TAC) was increased the 8 digit. There are free and commercial TAC lists exist on the internet.

it could be an option to validate TAC number by looking up the TAC lists on top of Luhn algorithm validation for new and old IMEI numbers. For old IMEI numbers FAC as 2 digit should be discarded and TAC validation should be done for 6 digits.



来源:https://stackoverflow.com/questions/2516579/check-for-valid-imei

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!