Pasal
 

Home
Up
Time-clock, Attendance
Web Data Extractor
Polaris FTP
Polaris Stamp
Polaris Word Count
AS/400 Data Browser
PC File CR Inserter
PC File Merger
Order via PayPal
Purchase Order
Free Downloads
Support Request
Contact Us

Copyright © 2004
by Polaris Computing.
All rights reserved.

 

 

Pascal Code for ISBN Conversion

The following program converts a 10-character ISBN to a 13-digit ISBN with the "978" prefix, and vise versa. The function ISBN1013 can be used to convert your currently existing ISBN's into the new format.

You can use Borland Delphi to compile the following code into a console application.  Minor modifications might be needed if you use other compilers.  The main program simply passes an argument to one of the conversion functions, ISBN1310 and ISBN1013, and conversions are all done by these functions.

Language Pascal
Compiler Borland Delphi
Disclaimer:  All sample code contained herein is provide to you  "AS IS."  ALL IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF THE MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE EXPRESSLY DISCLAIMED.

(*
This program gets an argument from the command line.
If the argument is a 10-character SIBN, the program will
print the corresponding 13-gigit ISBN. If the argument
is a 13-digit ISBN, the program will print the
corresponding 10-character ISBN.
The main program only provides a way of testing . All the
conversion is done by the two functions: ISBN1310
and ISBN1013.
*)

program isbnconv;
{$APPTYPE CONSOLE}
uses
  SysUtils;
var
  ISBN: string;

(********************* Convert ISBN-13 to ISBN-10 **********************)
function ISBN1310(ISBN: string): string;
var
  s9: string[9];
  s10: string[10];
  i, n: integer;
  ErrorOccurred: boolean;
begin
  ErrorOccurred := FALSE;
  s9 := Copy(ISBN, 4, 9);
  n := 0;
  for i:=1 to 9 do begin
    if NOT ErrorOccurred then begin
      if s9[i] IN ['0'..'9'] then n := n + (11-i)*(Ord(s9[i])-Ord('0'))
      else ErrorOccurred := TRUE;
    end;
  end;
  if ErrorOccurred then s10 := 'ERROR'
  else begin
    n := 11 - n mod 11;
    if n=11 then s10 := s9 + '0'
    else if n=10 then s10 := s9 + 'X'
    else s10 := s9 + Char(Ord('0') + n);
  end;
  ISBN1310 := s10;
end;

(********************* Convert ISBN-10 to ISBN-13 **********************)
function ISBN1013(ISBN: string): string;
var
  s12: string[12];
  s13: string[13];
  i, n: integer;
  ErrorOccurred: boolean;
begin
  ErrorOccurred := FALSE;
  s12 := '978' + Copy(ISBN, 1, 9);
  n := 0;
  for i:=1 to 12 do begin
    if NOT ErrorOccurred then begin
      if s12[i] IN ['0'..'9'] then begin
        if i mod 2 = 0 then n := n + 3*(Ord(s12[i]) - Ord('0'))
        else n := n + Ord(s12[i]) - Ord('0');
      end
      else ErrorOccurred := TRUE;
    end;
  end;
  If ErrorOccurred then s13 := 'ERROR'
  else begin
    n := n mod 10;
    if n<>0 then n := 10 - n;
    s13 := s12 + Char(Ord('0') + n);
  end;
  ISBN1013 := s13;
end;

(**************************** Main Program *****************************)
begin
  if ParamCount=1 then begin
    ISBN := ParamStr(1);
    if Length(ISBN)=10 then WriteLn(ISBN1013(ISBN))
    else if Length(ISBN)=13 then WriteLn(ISBN1310(ISBN))
    else WriteLn('Invalid ISBN.');
  end
  else WriteLn('USAGE: isbnconv ISBN');
end.