Converting ISBN13 to ISBN10
Thursday, January 28th, 2010 -- By ETI 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;
}


