### 1. Converting/Verifying ISBN numbers

hi,

i needed to verify and convert ISBN numbers. maybe someone can use the routines.

richard

```-- Calculate an ISBN13-13 checkdigit from the first 12 digits in an ISBN13-13 string
-- param: isbn - the first 12 digits of an ISBN13-13.
-- return: checkdigit (last digit) for an ISBN13-13
function genchksum13(string isbn)
integer oddNumbs = 0
integer evenNumbs = 0
integer checksum1 = 0
for i = 1 to 12 by 2 do
oddNumbs += (isbn[i]-48)
end for
for j = 2 to 12 by 2 do
evenNumbs += (isbn[j]-48)
end for
checksum1 = mod((oddNumbs + (evenNumbs * 3)), 10)
if checksum1 != 0 then
checksum1 = 10 - checksum1
end if
end function

-- Calculate an ISBN13-10 checkdigit from the first 9 digits in an ISBN13-10 string
-- param: isbn - the first 9 digits of an ISBN13-10.
-- return: checkdigit (last digit) for an ISBN13-10
function genchksum10(string isbn)
integer checkDigit = 0
for i = 1 to 9 do
checkDigit += ((10 - i + 1) *  (isbn[i]-48))
end for
checkDigit = 11 - mod(checkDigit, 11)
string checksum1 = to_string(checkDigit)
if checkDigit = 10 then
checksum1 = "X"
else
if checkDigit = 11 then
checksum1 = "0"
end if
end if
return checksum1
end function

--Validate an ISBN13-10 using the checkdigit (last digit)
--param: isbn - ISBN13-10
function validISBN10(string isbn)
if upper(genchksum10(isbn[1..9])) = upper(isbn[10]&"") then
return true
else
return false
end if
end function

--Validate an ISBN13-13 using the checkdigit (last digit)
--param: isbn - ISBN13-13
function validISBN13(string isbn)
string checksum1 = genchksum13(isbn[1..12])
if checksum1 = isbn[13]&"" then
return true
else
return false
end if
end function

-- Convert an ISBN13-10 to ISBN13-13 or ISBN13-13 to ISBN13-10.
-- Note: Only ISBN13-13 that start with 978 can be converted to ISBN13-10, as ISBN13-13 begining with other
-- digits will not result in a unique ISBN13-10 (i.e. they are not backward convertible)
-- param: ISBN13-10 or ISBN13-13
-- return: a string containing the ISBN13-10, ISBN13-13 or an error message
function convertISBN(string isbn)
if length(isbn) = 13 then
if isbn[1..3] = "978" then
if validISBN13(isbn) = true then
return isbn[4..\$-1] & genchksum10(isbn[4..\$])
else
return "Not a valid ISBN13"
end if
else
return "ISBN13-13 must begin with 978"
end if
elsif length(isbn) = 10 then
if isbn[1] = 'B' then
return isbn
end if
if validISBN10(isbn) then
return "978" & isbn[1..9] & genchksum13("978" & isbn[1..9])
else
return "Not a valid ISBN10"
end if
else
return ""
end if
end function

--?validISBN13("9783442151479")
--?validISBN10("3442151473")
--?convertISBN("9783442151479")
--?convertISBN("3442151473")
```