














Copyright © 2004
by Polaris Computing.
All rights reserved.
| |
| |
RPG 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 IBM AS/400 RPG compiler to compile
the following code. 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 |
RPG |
|
Compiler |
IBM AS/400 RPG |
|
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. |
*************** Beginning of data
***************************
H* This program gets an argument from the command line.
H* If the argument is a 10-character ISBN, the program will
H* print the corresponding 13-digit ISBN. If the argument
H* is a 13-digit ISBN, the program will print the
H* corresponding 10-character ISBN.
H* The main program is only for testing. All the
H* conversion is done by the two functions: ISBN1310
H* and ISBN1013.
H*
DISBN1013 PR
13A
DISBN0
10A Value
DISBN1310 PR
10A
DISBN0
13A Value
DISBN0
S
13A
C*
C********************* The Main Program ********************
C *Entry
PList
C
Parm
ISBN0
/free
If %Parms=1;
If %Len(%Trim(ISBN0))=10;
Dsply ISBN1013(%Trim(ISBN0));
ElseIf %Len(%Trim(ISBN0))=13;
Dsply ISBN1310(%Trim(ISBN0));
Else;
Dsply 'Invalid ISBN.';
EndIf;
Else;
Dsply 'Usage: CALL ISBNTEST PARM(isbn)';
EndIf;
*INLR = *ON;
Return;
/end-free
P*
P*************** Convert ISBN-13 to ISBN-10 ****************
PISBN1310 B
DISBN1310 PI
10A
DISBN0
13A Value
DISBN1
S
10A
Ds9
S
9A
Di
S
2P 0
Dn
S
4P 0
Dch
S
1A
Dcv
S
1P 0
/free
ISBN1 = '';
s9 = %SubSt(ISBN0:4:9);
n = 0;
For i = 1 to 9;
If ISBN1='';
ch = %SubSt(s9:i:1);
If (ch>='0') AND (ch<='9');
cv = %Dec(ch:1:0);
n = n + (11 - i)*cv;
Else;
ISBN1 = 'ERROR';
EndIf;
EndIf;
EndFor;
If ISBN1='';
n = 11 - %Rem(n:11);
If n=11;
ch='0';
ElseIf n=10;
ch='X';
Else;
ch=%Char(n);
EndIf;
ISBN1 = s9 + ch;
EndIf;
Return ISBN1;
/end-free
P E
P*
P*************** Convert ISBN-10 to ISBN-13 ****************
PISBN1013 B
DISBN1013 PI
13A
DISBN0
10A Value
DISBN1
S
13A
Di
S
2P 0
Dn
S
4P 0
Dch
S
1A
Dcv
S
1P 0
Ds12
S
12A
/free
ISBN1 = '';
s12 = '978' + %SubSt(ISBN0:1:9);
n = 0;
For i = 1 to 12;
If ISBN1='';
ch = %SubSt(s12:i:1);
If ch>='0' AND ch<='9';
cv = %Dec(ch:1:0);
If %Rem(i:2)=0;
n = n + 3*cv;
Else;
n = n +cv;
EndIf;
Else;
ISBN1 = 'ERROR';
EndIf;
EndIf;
EndFor;
If ISBN1='';
n = %Rem(n:10);
If n<>0;
n = 10 - n;
EndIf;
ch = %Char(n);
ISBN1 = s12 + ch;
EndIf;
Return ISBN1;
/end-free
P E
****************** End of data ******************************
|
|
|