; CODICE32
;
; Program for Proteus
;
; (C) 2003-2004 Simone Zanella Productions
;
; Functions for encoding/decoding Code 32 (Italian Pharmaceutical).
FUNCTION Conv32(cod)
; Alphabet for Code 32 calculation
base32 = "0123456789BCDFGHJKLMNPQRSTUVWXYZ"
; Calculate check digit
c = C32CheckDigit(cod)
d = ""
; Transform number into the alphanumeric string
; corresponding to Code 32 representation
FOR x = 1 TO 6
d = SUBSTR(base32, INC(MOD(c, 32)), 1) d
DIV(@c, 32)
NEXT
; Return new code
RETURN d
FUNCTION C32CheckDigit(cod)
; Calculate check digit on cod, return the string with the added check digit
p = 0
FOR x = 2 TO 8 STEP 2
c = MUL(SUBSTR(cod, x, 1), 2)
ADD(@p, DIV(c, 10), MOD(c, 10))
NEXT
FOR x = 1 TO 7 STEP 2
ADD(@p, SUBSTR(cod, x, 1))
NEXT
RETURN cod MOD(p, 10)
FUNCTION ReConv32(cod)
; Return the numeric code corresponding to cod (Code 32)
; Alphabet for Code 32 calculation
base32 = "0123456789BCDFGHJKLMNPQRSTUVWXYZ"
c = 0
FOR x = 1 TO 6
d = SUBSTR(cod, x, 1)
p = STRSTR(base32, d)
c = ADD(MUL(c, 32), DEC(p))
NEXT
RETURN PADL(c, 9, "0")
CONSOLELN "02608901" ", " Conv32("02608901") ", " ReConv32(Conv32("02608901"))
CONSOLELN "12358231" ", " Conv32("12358231") ", " ReConv32(Conv32("12358231"))
CONSOLELN "44812722" ", " Conv32("44812722") ", " ReConv32(Conv32("44812722"))
CONSOLELN "12456762" ", " Conv32("12456762") ", " ReConv32(Conv32("12456762"))
CONSOLELN "12345678" ", " Conv32("12345678") ", " ReConv32(Conv32("12345678"))
CONSOLELN "90206826" ", " Conv32("90206826") ", " ReConv32(Conv32("90206826"))
ABORT 0