Calculating Code 128 Barcode Checksum in FoxPro
Home >
Barcode Basics >
Application Notes >
AppNote001This FoxPro code will calculate a Code 128 checksum for Subset B and format the final
string. Barcode fonts from different publishers may map the start and stop
characters to different locations, so be sure to check.
The general method involves pre-loading a subtotal to the numeric value of the Code 128
Subset B start character (104). Then scan the data string, multiplying the numeric
value of each character by its position in the string (1st character * 1, 2nd character *
2, etc.) and adding the result to the running subtotal. Then perform a Modulo 103
division (divide by 103 and take the remainder) on the subtotal and convert the result to
a character.
*
* Initialize running total with value of start character, then scan the
* string and add character value times position
*
SUBTOT = 104
FOR I = 1 TO LEN(MYSTRING)
C = SUBSTR(MYSTRING, I, 1)
X = VAL(C) - 32
SUBTOT = SUBTOT + (X * I)
NEXT I
*
* Calculate Modulo 103. This is the checksum
*
CHKDIG = MOD(SUBTOT, 103)
*
* Now convert numeric checksum to a character. This conversion takes
* into account the particular mapping of the font being used (this
* example is for the font published by Azalea Software).
*
DO CASE
CASE CHKDIG = 0
CHKCHR = CHR(174)
CASE CHKDIG < 94
CHKCHR = CHR(CHKDIG + 32)
OTHERWISE
CHKCHR = CHR(CHKDIG + 71)
ENDCASE
*
* Now format the final output string: start character,
* data, check character, and stop character.
*
OUTSTRING = CHR(162) + MYSTRING + CHKCHR + CHR(164)