Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

any code handy to convert isbn-10 to isbn-13

8 views
Skip to first unread message

keithv

unread,
Aug 12, 2011, 4:12:31 PM8/12/11
to
Hi,

Before I re-invent the wheel, does anybody have any code lying
around that converts ISBN-10 to ISBN-13?

Thanks,
Keith

Harald Oehlmann

unread,
Aug 14, 2011, 2:44:04 PM8/14/11
to

IMHO you need:
- the ISBN prefix: 978
- the first 9 digits of the ISBN-10
- the newly calculated EAN-13 check digit.
Here is code for the check digit calculation:

# Find the EAN 8/13/SSCC check digit
proc CheckEAN In {
set Sum 0
set Pos [string length $In]
set fWeight 0
while { $Pos > 0 } {
incr Pos -1
set Num [string index $In $Pos]
incr Sum $Num
if {$fWeight} {
set fWeight 0
} else {
incr Sum $Num
incr Sum $Num
set fWeight 1
}
}
return [expr { ( 10 - ( $Sum % 10 ) ) % 10 } ]
}

Hope this helps,
Harald

Andreas Kupries

unread,
Aug 16, 2011, 1:55:48 AM8/16/11
to
keithv <kve...@gmail.com> writes:

> Hi,
>
> Before I re-invent the wheel, does anybody have any code lying
> around that converts ISBN-10 to ISBN-13?

Look into Tcllib, the snit validation types I made (1). The isbn code
also have a "13of" method which does the 10->13 conversion, as part of
isbn canonicalization.

(1) subdirectory modules/valtype

The code of the method

typemethod 13of {value} {
if {![regexp {^[0-9]+[Xx]?$} $value]} {
badchar ISBN "ISBN-10 number, expected only digits, and possibly 'X' or 'x' as checkdigit"
} elseif {[string length $value] != 10} {
badlength ISBN 10 "ISBN-10 number"
}

# Strip the -10 check digit, prefix the remainder with the
# bookland country code and recalculate the check digit, via
# -13.

set n 978[string range $value 0 end-1]
return $n[$type checkdigit $n]
}

HTH.

> Thanks,
> Keith

--
So long,
Andreas Kupries <akup...@shaw.ca>
<http://www.purl.org/NET/akupries/>
Developer @ <http://www.activestate.com/>
-------------------------------------------------------------------------------

0 new messages