














Copyright © 2004
by Polaris Computing.
All rights reserved.
| |
| |
BASIC Code for ISBN
Conversion
The following program converts a 10-character
ISBN to a 13-digit
ISBN with the "978" prefix, and vise versa. The
function ISBN1013 can be used to convert your currently existing ISBN's into
the new format.
You can use the following code with Microsoft
Access. Modifications might be needed if you use other
compilers or interpreters. The button click event handler simply passes the input to one of the conversion functions,
ISBN1310 and ISBN1013, and conversions are all done by these functions.
|
Language |
BASIC |
|
Interpreter |
Microsoft Access VBA |
|
Disclaimer: All sample
code contained herein is provide to you "AS IS." ALL IMPLIED
WARRANTIES, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF THE
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE EXPRESSLY
DISCLAIMED. |
Option Compare Database
Option Explicit
' This is the source code for a Microsoft Access form.
' There are two text boxes (TextISBN1 and TextISBN2) and
' a button (CommandConvert) on the form.
' When the user enters an ISBN in TextISBN1 and then
' click on CommandConvert, the program will convert the
' ISBN and display the result in Text ISBN2.
' If TextISBN1 has a 10-character ISBN, TextISBN2 will
' show the corresponding 13-digit ISBN. If TextISBN1 has
' a 13-digit ISBN, TextISBN2 wil show the corresponding
' 10-character ISBN.
' The CommandConvert_Click event handler only provides a
' testing routine. All the conversion is done by one of
' the functions: ISBN1310 and ISBN1013.
''''''''''''''''''''''
Convert ISBN-13 to ISBN-10 '''''''''''''''''''''''
Private Function ISBN1310(ByVal ISBN As String) As String
Dim s9, s10, c As String
Dim i, n As Integer
Dim ErrorOccurred As Boolean
ErrorOccurred = False
s9 = Mid(ISBN, 4, 9)
n = 0
For i = 1 To 9
If Not ErrorOccurred Then
c = Mid(s9, i, 1)
If (c >= "0") And (c <= "9") Then
n = n + (11 - i) * (Asc(c) - Asc("0"))
Else
ErrorOccurred = True
End If
End If
Next i
If ErrorOccurred Then
s10 = "ERROR"
Else
n = 11 - n Mod 11
If n = 11 Then
s10 = s9 & "0"
ElseIf n = 10 Then
s10 = s9 & "X"
Else
s10 = s9 & Chr(n + Asc("0"))
End If
End If
ISBN1310 = s10
End Function
'''''''''''''''''''''' Convert ISBN-10 to ISBN-13
'''''''''''''''''''''''
Private Function ISBN1013(ByVal ISBN As String) As String
Dim s12, s13, c As String
Dim i, n As Integer
Dim ErrorOccurred As Boolean
ErrorOccurred = False
s12 = "978" & Left(ISBN, 9)
n = 0
For i = 1 To 12
If Not ErrorOccurred Then
c = Mid(s12, i, 1)
If (c >= "0") And (c <= "9") Then
If i Mod 2 = 0 Then
n = n + 3 * (Asc(c) - Asc("0"))
Else
n = n + Asc(c) - Asc("0")
End If
Else
ErrorOccurred = True
End If
End If
Next i
If ErrorOccurred Then
s13 = "ERROR"
Else
n = n Mod 10
If n <> 0 Then n = 10 - n
s13 = s12 & Chr(n + Asc("0"))
End If
ISBN1013 = s13
End Function''''''''''''''''''''''' Button
Click Event Handler ''''''''''''''''''''''
Private Sub CommandConvert_Click()
TextISBN1 = Trim(TextISBN1)
If Len(TextISBN1) = 10 Then
TextISBN2 = ISBN1013(TextISBN1)
ElseIf Len(TextISBN1) = 13 Then
TextISBN2 = ISBN1310(TextISBN1)
Else
TextISBN2 = "Invalid ISBN"
End If
End Sub
|
|
|