[Web4lib] Conversion between ISBN-10 and ISBN-13

Paul F. Schaffner pfs at umich.edu
Sun Oct 1 14:23:22 EDT 2006


Patricial Anderson wrote:

> To go from 13 to 10 is easy -- the isbn-10 is the last 10 digits of the 
> isbn-13.

No it's not. As long as we are still within the 978- range of ISBN-13s,
the first nine digits of the ISBN-10 and the penultimate nine
digits of the ISBN 13 should match, but the final digit (the check
digit) will normally *not* be the same.

Below are some crude perl scripts I wrote to convert lists of ISBN-10s to
ISBN-13 and 978-prefixed ISBN-13s to ISBN-10. My apologies for the 
crudity of the perl--it was just for fun, in the interests of 
understanding (and making some marginal improvements in my perl skills).
However, their very verbosity may make the basics of the conversion clear.
pfs
---------------------------------------------------------------------
Paul Schaffner | pfs at umich.edu | http://www-personal.umich.edu/~pfs/
Digital Library Production Service, University of Michigan Libraries
--------------------------------------------------------------------


# ----beginning of script ---
# FILENAME make_isbn13.pl
# Input is text file containing ISBN-10s, one ISBN per line.
# Output is text file containing hash of ISBN-10s paired with equivalent 
# ISBN-13s.
# Sample usage: C:\perl make_isbn13.pl ISBN10s.txt > ISBN13s.txt
# Will fail silently on ISBNs that do not contain 10 digits.
# Does not attempt to check if the ISBN10s are valid otherwise.
# More elaborate and elegant code can be found in the Mana Systems perl 
# module:
# http://www.manasystems.co.uk/isbnpm.html
# --pfs

while (<>) {

   s,-,,g;
   s, +,,g;
   s,$,pfs,g;
   s,x,X,g;

   while 
(s,([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9X])pfs,,g) 
{

       $a = $1 * 3;
       $b = $2;
       $c = $3 * 3;
       $d = $4;
       $e = $5 * 3;
       $f = $6;
       $g = $7 * 3;
       $h = $8;
       $i = $9 * 3;
       $check = 38 + $a + $b + $c + $d + $e + $f + $g + $h + $i;
       $check %= 10;
       $check = 10 - $check;

         if ($check == 10)
           { $check = 0 };

        print ("\n$1$2$3$4$5$6$7$8$9$10, 978$1$2$3$4$5$6$7$8$9$check");

}
}

# ---end of script----

------------------------------------------

# ----beginning of script ---
# FILENAME make_isbn10.pl
# Input is text file containing ISBN-13s, one ISBN per line.
# Output is text file containing hash of (978-prefixed) ISBN-13s paired 
# with equivalent ISBN-10s.
# Sample usage: C:\perl make_isbn10.pl ISBN13s.txt > ISBN10s.txt
# Will fail silently on ISBNs that do not contain 13 digits or do not 
# begin with 978-.
# Does not attempt to check if the ISBN13s are valid otherwise.
# More elaborate and elegant code can be found in the Mana Systems perl 
# module:
# http://www.manasystems.co.uk/isbnpm.html
# --pfs

while (<>) {

   s,-,,g;
   s, +,,g;
   s,$,pfs,g;

   while 
(s,978([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])pfs,,g) 
{

       $a = $1;
       $b = $2 * 2;
       $c = $3 * 3;
       $d = $4 * 4;
       $e = $5 * 5;
       $f = $6 * 6;
       $g = $7 * 7;
       $h = $8 * 8;
       $i = $9 * 9;
       $check = $a + $b + $c + $d + $e + $f + $g + $h + $i;
       $check %= 11;

         if ($check == 10)
           { $check = X };

        print ("\n978$1$2$3$4$5$6$7$8$9$10, $1$2$3$4$5$6$7$8$9$check");

}
}

# ---end of script----






More information about the Web4lib mailing list