Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to validate ISBN's? Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
12-Oct-02
Category
Algorithm
Language
Delphi 3.x
Views
80
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Fernando Silva

ISBNs (or International Standard Book Numbers) are mystical code numbers that 
uniquely identify books. The purpose of this article is to remove the mystery 
surrounding the structure of the ISBN, allowing applications to perform data 
validation on entered candidate ISBNs.

Answer:

ISBNs are composed of thirteen characters, limited to the number characters "0" 
through "9", the hyphen, and the letter "X". This thirteen-character code is 
divided into four parts, each separated by hyphens: group identifier, publisher 
identifier, book identification for the publisher, and the check digit. The first 
part (group identifier) is used to identify countries, geographical regions, 
languages, etc. The second part (publisher identifier) uniquely identifies the 
publisher. The third part (book identifier) uniquely identifies a given book within 
a publisher's collection. The fourth and final part (check digit) is used with the 
other digits in the code in an algorithm to derive a verifiable ISBN. The number of 
digits in the first three parts of an ISBN may contain a variable number of digits, 
but the check digit will always consist of a single character (between "0" and "9", 
or "X" for a value of 10) and the
ISBN as a whole will always consists of thirteen characters (ten numbers plus the 
three hyphens dividing the four parts of the ISBN).

The ISBN 3-88053-002-5 breaks down into the parts:

  Group:       3
  Publisher:   88053
  Book:        002
  Check Digit: 5

An ISBN can be verified to be a valid code using a simple mathematical algorithm. 
This algorithm takes each of the nine single digits from the first three parts if 
the ISBN (sans the non-numeric hyphens), multiplies each single digit by a number 
that is less than eleven the number of positions from the left each digit that is 
in the ISBN, adds together the result of each multiplication plus the check digit, 
and then divides that number by eleven. If that division by eleven results in no 
remainder (i.e., the number is modulo 11), the candidate ISBN is a valid ISBN. For 
example, using the previous sample ISBN 3-88053-002-5:

  ISBN:              3  8  8  0  5  3  0  0  2  5
  Digit Multiplier: 10  9  8  7  6  5  4  3  2  1
  Product:          30+72+64+00+30+15+00+00+04+05 = 220

Since 220 is evenly divisible by eleven, this candidate IDBN is a valid ISBN code.

This verification algorithm is easily translated into Pascal/Delphi code.
String manipulation functions and procedures are used to extract the check digit 
and the remainder of the ISBN from the String type value passed to a validation 
function. The check digit is converted to Integer type, which forms the start value 
of the aggregate variable onto which the multiplication of each digit in the 
remainder of the ISBN (the single digits that comprise the first three parts of the 
ISBN) will be added. A For loop is used to sequentially process each digit in the 
remainder, ignoring the hyphens, multiplying each digit times its position in the 
ISBN remainder relative to the other digits in the remainder. The final value of 
this aggregate variable is then checked to see whether it is evenly divisible by 
eleven (indicating a valid ISBN) or not (indicating an invalid candidate ISBN).

Here is an example of this methodology applied in a Delphi function:
1   
2   function IsISBN(ISBN: string): Boolean;
3   var
4     Number, CheckDigit: string;
5     CheckValue, CheckSum, Err: Integer;
6     i, Cnt: Word;
7   
8   begin
9     // Get check digit
10    CheckDigit := Copy(ISBN, Length(ISBN), 1);
11    // Get rest of ISBN, minus check digit and its hyphen
12    Number := Copy(ISBN, 1, Length(ISBN) - 2);
13    // Length of ISBN remainder must be 11 and check digit between 9 and 9 or X
14    if (Length(Number) = 11) and (Pos(CheckDigit, '0123456789X') > 0) then
15    begin
16      // Get numeric value for check digit
17      if (CheckDigit = 'X') then
18        CheckSum := 10
19      else
20        Val(CheckDigit, CheckSum, Err);
21      // Iterate through ISBN remainder, applying decode algorithm
22      Cnt := 1;
23      for i := 1 to 12 do
24      begin
25        // Act only if current character is between "0" and "9" to exclude hyphens
26        if (Pos(Number[i], '0123456789') > 0) then
27        begin
28          Val(Number[i], CheckValue, Err);
29          // Algorithm for each character in ISBN remainder, Cnt is the nth character
30          //    so processed
31          CheckSum := CheckSum + CheckValue * (11 - Cnt);
32          Inc(Cnt);
33        end;
34      end;
35      // Verify final value is evenly divisible by 11
36      if (CheckSum mod 11 = 0) then
37        IsISBN := True
38      else
39        IsISBN := False;
40    end
41    else
42      IsISBN := False;
43  end;


This is a simplified example, kept simple to best demonstrate the algorithm to decode ISBNs. There are a number of additional features that would be desirable to add for use in a real-world application. For instance, this example function requires the candidate ISBN be passed as a Pascal String type value, with the hyphens dividing the four parts of the ISBN. Added functionality might accommodate evaluating candidate ISBNs entered without the hyphens. Another feature that might be added is checking that ensures three hyphens are properly included, as opposed to just thirteen number characters.

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC