Awhile ago I wrote some functions to convert from isbn10 to isbn13, or from isbn13 to isbn10; and subsequently some function to validate isbn 10/13 numbers.
I thought I'd post them here to see what people think of them.
So,
' Validates an ISBN13 number. If false, it's invalid, else if true, it's valid,
Function bVerifySum13(ByRef s As String) As Boolean
If Not (s Like "#############" And Mid(s, 1, 3) = "978") Then
Return False
End If
Dim i As Integer = 30
For j As Integer = 4 To 12 Step 2
i += Convert.ToInt32(Mid(s, j - 1, 1)) + (3 * Convert.ToInt32(Mid(s, j, 1)))
Next
If Not (Mid(s, 13, 1) = ((10 - (i Mod 10)) Mod 10).ToString) Then
Return False
End If
Return True
End Function
' Conversion from ISBN13 to ISBN10
Function sConvertN13toN10(ByRef s As String) As String
sConvertN13toN10 = Mid(s, 4, 9)
Dim i As Integer = 0
For j As Integer = 10 To 2 Step -1
i += (j * Convert.ToInt32(Mid(sConvertN13toN10, (10 - (j - 1)), 1)))
Next
If (i Mod 11) = 0 Then
Return sConvertN13toN10 + "0"
End If
Dim iCount As Integer = 1
Do Until ((i + iCount) Mod 11) = 0
iCount += 1
Loop
If iCount = 10 Then
Return sConvertN13toN10 + "X"
End If
Return sConvertN13toN10 + iCount.ToString
End Function
' Validates an ISBN10 number. If false, it's invalid, else if true, it's valid,
Function bVerifySum10(ByRef s As String) As Boolean
Dim i As Integer = 0
For j = 10 To 2 Step -1
i += ((Convert.ToInt32(Mid(s, (10 - (j - 1)), 1))) * j)
Next
If (i Mod 11) = 0 And Mid(s, 10, 1) = "0" Then
Return True
ElseIf Mid(s, 10, 1).ToUpper = "X" Then
i += 10
Else
i += Convert.ToInt32(Mid(s, 10, 1))
End If
If Not ((i Mod 11) = 0) Then
Return False
End If
Return True
End Function
' Conversion from ISBN10 to ISBN13
Function sConvertN10toN13(ByRef s As String) As String
Dim i As Integer = 30
sConvertN10toN13 = "978" + Mid(s, 1, 9)
For j As Integer = 4 To 12 Step 2
i += Convert.ToInt32(Mid(sConvertN10toN13, j - 1, 1)) + (3 * Convert.ToInt32(Mid(sConvertN10toN13, j, 1)))
Next
Return sConvertN10toN13 + ((10 - (i Mod 10)) Mod 10).ToString
End Function