VIN 使用第 9 位的校验位和您可以很容易找到的公式进行验证。这是我编写的验证的 VB 版本。我是一个新的 VB 程序员,所以我确信有更优雅的方法,但逻辑是正确的......“all one's”(见注释行)是对任何 VIN 验证算法的一个很好的检查。
Function ValidateVin(vin As String) As Boolean
'Dim vin As String = "2G1FC3D33C9165616"
'Dim vin As String = "11111111111111111"
Dim vinCharArray() = vin.ToCharArray()
Dim result As Int32
Dim index As Int32 = 0
Dim checkDigit As Int32
Dim checkSum As Int32 = 0
Dim weight As Int32
If vin.Length <> 17 Then
Console.WriteLine(vbCrLf + "Supplied VIN does not have 17 characters, please check and try again" + vbCrLf)
Return False
Else
For Each i As Char In vinCharArray
index += 1
If Asc(i) > 47 And Asc(i) < 58 Then
result = Int32.Parse(i)
Else
Select Case Char.ToLower(i)
Case "a", "j"
result = 1
Case "b", "k", "s"
result = 2
Case "c", "l", "t"
result = 3
Case "d", "m", "u"
result = 4
Case "e", "n", "v"
result = 5
Case "f", "w"
result = 6
Case "g", "p", "x"
result = 7
Case "h", "y"
result = 8
Case "r", "z"
result = 9
End Select
End If
Select Case index
Case 1 To 7, 9
weight = 9 - index
Case 8
weight = 10
Case 10 To 17
weight = 19 - index
End Select
If index = 9 Then
If Char.ToLower(i) = "x" Then
checkDigit = 10
Else
checkDigit = result
End If
End If
'Console.WriteLine("Index {0} has a value of {1} and a weight of {2}", index, result, weight)
checkSum += (result * weight)
Next
'Console.WriteLine("checksum is {0}", checkSum)
'Console.WriteLine("checkdigit is {0}", checkDigit)
Dim checksOut As Boolean
If checkSum Mod 11 = checkDigit Then
checksOut = True
End If
'Console.WriteLine(checksOut)
'Console.ReadLine()
Return checksOut
End If
End Function