Converting ISBN13 to ISBN10

By ET

I need to convert a bunch of isbn numbers from the 13-digit format to the 10-digit format.

Figured out the algorithm, and implemented the following subroutine in PERL. It should be fairly simple to port it to other languages.

sub isbn1310{

my $isbn13=shift;
my @isbn13=split(//,$isbn13);

my $sum = ($isbn13[3] * 10) + (9 * $isbn13[4]) + (8 * $isbn13[5]) + (7 * $isbn13[6]) + (6 * $isbn13[7]) + (5 * $isbn13[8]) +
(4 * $isbn13[9]) + (3 * $isbn13[10]) + (2 * $isbn13[11]);

my $mode = int(($sum / 11) + 1);
my $last = (11 * $mode) - $sum;
if ($last == 10) {
$last = "X";
} elsif ($last == 11) {
$last = 0;
}

my $isbn10="$isbn13[3]"."$isbn13[4]"."$isbn13[5]"."$isbn13[6]"."$isbn13[7]"."$isbn13[8]"."$isbn13[9]"."$isbn13[10]"."$isbn13[11]"."$last";
return $isbn10;
}

For completeness, here is the program to convert ISBN10 back ISBN13

sub isbn1013{

my $isbn10=shift;
my @isbn10=split(//,$isbn10);

my $n = 9+7*3+8+$isbn10[0]*3 + $isbn10[1] + 3 * $isbn10[2] + $isbn10[3] + 3 * $isbn10[4] + $isbn10[5] + 3 * $isbn10[6] + $isbn10[7] + 3 * $isbn10[8];

my $n2 = $n % 10;

my $o = 10 - $n2;

my $isbn13="978"."$isbn10[0]"."$isbn10[1]"."$isbn10[2]"."$isbn10[3]"."$isbn10[4]"."$isbn10[5]"."$isbn10[6]"."$isbn10[7]"."$isbn10[8]"."$o";
return $isbn13;
}

  1. 1
    ET Says:

    There is actually a perl module for this:

    use Business::ISBN;

    # 10 digit ISBNs
    $isbn10 = Business::ISBN->new(’1565922573′);
    $isbn10 = Business::ISBN->new(’1-56592-257-3′);

    # 13 digit ISBNs
    $isbn13 = Business::ISBN->new(’978-0-596-52724-2′);

    # convert
    $isbn10 = $isbn13->as_isbn10; # for the 978 prefixes

    $isbn13 = $isbn10->as_isbn13;

Leave a Reply


BlogTimer
You are visitor number several since September 1, 2001

Copyright Xiaoquan (Michael) Zhang, 2004-2010. All rights reserved.
All trademarks property of their owners.