ASP: ISBN Conversion
Introduction
Author: Martin WarningYear: 2007
License: Free
This code shows examples on how to convert an ISBN10 number to the new ISBN13 number. It also show how to convert ISBN13 to ISBN10, but take in account that this only works for those ISBN13 numbers starting with 978. All other ISBN13'a can't be converted to ISBN10 values.
The following example shows a few example codes on how to implement both ISBN conversions on your site. This code can be useful for those who have an ISBN search or wish to show both ISBN values on their site while only storing one in the database. Important to take in account when working with ISBN's is that the last digit of both ISBN10 and ISBN13 are a control digit that needs to be calculated. We won't go into details of the formula, but one should consider this in any ISBN search system. Some publisher have the wrong ISBN (last digit) listed in their book. In order to offer the best chance of a user finding a book only the first 9 (ISBN10) or 12 (ISBN13) digits should be used in an ISBN search. If you take this into account and the fact the first 9 digits of ISBN10 are equal to digit 4-12 of ISBN13 are equal any conversion not taking the last digit into account for use in a search is very straightforward. In the conversion examples will include a last digit calculator. As last we include a demo and as download an example of ISBN10 or ISBN13 to both ISBN10 and ISBN13 conversion and last digit calculator.
Code
Languages:- ASP
- VBScript
<% ' Declare and initialize the variables Dim strISBN10, strISBN10New Dim i Dim intCalc, intCalc2, intCalc3, strCalc4 For i = 1 To 9 If isNumeric(Mid(strISBN10,i,1)) Then intCalc = intCalc + (Mid(strISBN10,i,1) * (11-i)) End If Next intCalc2 = Split((intCalc / 11),".") intCalc3 = intCalc - (intCalc2(0)*11) strCalc4 = 11 - intCalc3 If strCalc4 = 10 Then strCalc4 = "x" ElseIf strCalc4 = 11 Then strCalc4 = "0" End If strISBN10New = Left(strISBN10,9) & strCalc4 %>
You will need to load the strISBN10 variable with the ISBN you want to use to calculate the last digit. The intISBN10New variable will have the result.
The following code is an example on how to calculate the last digit of an ISBN13.
<% ' Declare and initialize the variables Dim intISBN13, intISBN13New Dim i Dim intCalc, intCalc2, intCalc3, intCalc4, intMultiply intMultiply = 1 For i = 1 To 12 If isNumeric(Mid(intISBN13,i,1)) Then intCalc = intCalc + (Mid(intISBN13,i,1) * intMultiply) If intMultiply = 1 Then intMultiply = 3 Else intMultiply = 1 End If End If Next intCalc2 = Split((intCalc / 10),".") intCalc3 = intCalc - (intCalc2(0)*10) intCalc4 = 10 - intCalc3 If intCalc4 = 10 Then intCalc4 = "0" End If intISBN13New = Left(intISBN13,12) & intCalc4 %>
You will need to load the strISBN13 variable with the ISBN you want to use to calculate the last digit. The intISBN13New variable will have the result.
Now we can move on to doing some conversion. First the code to conver ISBN10 to ISBN13:
<% ' Declare and initialize the variables Dim strISBN10, intISBN13, intISBN13New Dim i Dim intCalc, intCalc2, intCalc3, intCalc4, intMultiply intISBN13 = 978 & Left(strISBN10,9) intMultiply = 1 For i = 1 To 12 If isNumeric(Mid(intISBN13,i,1)) Then intCalc = intCalc + (Mid(intISBN13,i,1) * intMultiply) If intMultiply = 1 Then intMultiply = 3 Else intMultiply = 1 End If End If Next intCalc2 = Split((intCalc / 10),".") intCalc3 = intCalc - (intCalc2(0)*10) intCalc4 = 10 - intCalc3 If intCalc4 = 10 Then intCalc4 = "0" End If intISBN13New = Left(intISBN13,12) & intCalc4 %>
The conversion will add the 978 digits in front of the first 9 digits of the ISBN10. All that remains is calculating the last digit of the ISBN13.
Example code to convert ISBN13 to ISBN10:
<% ' Declare and initialize the variables Dim strISBN10, strISBN10New, intISBN13 Dim i Dim intCalc, intCalc2, intCalc3, strCalc4 If Mid(intISBN13,1,3) = 978 Then strISBN10 = Right(Left(intISBN13,12),9) For i = 1 To 9 If isNumeric(Mid(strISBN10,i,1)) Then intCalc = intCalc + (Mid(strISBN10,i,1) * (11-i)) End If Next intCalc2 = Split((intCalc / 11),".") intCalc3 = intCalc - (intCalc2(0)*11) strCalc4 = 11 - intCalc3 If strCalc4 = 10 Then strCalc4 = "x" ElseIf strCalc4 = 11 Then strCalc4 = "0" End If strISBN10New = Left(strISBN10,9) & strCalc4 End If %>
This conversion will convert ISBN10 compatible ISBN13 values to ISBN10. If the ISBN13 is not ISBN10 compatible (ie doesn't start with 978) nothing is done. You can add an Else statement to the If Mid(intISBN13,1,3) = 978 statement if you wish to do something when the entered ISBN13 is not ISBN10 compatible.
The following example offers conversion from ISBN10 to ISBN13 as well as ISBN13 to ISBN10, it also checks if the entered ISBN is valid. The complete code is available for download. This code uses the examples shown above in a modified form.
