Code: Select all
FUNCTION _CODE128( cCode, cMode )
LOCAL aCode := {"212222",;
"222122",;
"222221",;
"121223",;
"121322",;
"131222",;
"122213",;
"122312",;
"132212",;
"221213",;
"221312",;
"231212",;
"112232",;
"122132",;
"122231",;
"113222",;
"123122",;
"123221",;
"223211",;
"221132",;
"221231",;
"213212",;
"223112",;
"312131",;
"311222",;
"321122",;
"321221",;
"312212",;
"322112",;
"322211",;
"212123",;
"212321",;
"232121",;
"111323",;
"131123",;
"131321",;
"112313",;
"132113",;
"132311",;
"211313",;
"231113",;
"231311",;
"112133",;
"112331",;
"132131",;
"113123",;
"113321",;
"133121",;
"313121",;
"211331",;
"231131",;
"213113",;
"213311",;
"213131",;
"311123",;
"311321",;
"331121",;
"312113",;
"312311",;
"332111",;
"314111",;
"221411",;
"431111",;
"111224",;
"111422",;
"121124",;
"121421",;
"141122",;
"141221",;
"112214",;
"112412",;
"122114",;
"122411",;
"142112",;
"142211",;
"241211",;
"221114",;
"213111",;
"241112",;
"134111",;
"111242",;
"121142",;
"121241",;
"114212",;
"124112",;
"124211",;
"411212",;
"421112",;
"421211",;
"212141",;
"214121",;
"412121",;
"111143",;
"111341",;
"131141",;
"114113",;
"114311",;
"411113",;
"411311",;
"113141",;
"114131",;
"311141",;
"411131",;
"211412",;
"211214",;
"211232",;
"2331112"}
LOCAL nSum, cBar, cCar
LOCAL cTemp, n, nCAr, nCount := 0
LOCAL lCodeC := .F., lCodeA := .F.
IF VALTYPE( cCode ) != "C" THEN RETURN NIL
IF !EMPTY( cMode )
IF VALTYPE( cMode ) = "C" .AND. UPPER( cMode ) $ "ABC"
cMode = UPPER( cMode )
ENDIF
ENDIF
IF EMPTY( cMode )
IF ISNUMBER( cCode )
lCodeC = .T.
cTemp = aCode[ 106 ]
nSum = 105
ELSE
FOR n = 1 TO LEN( cCode )
nCount += IF( SUBSTR( cCode, n, 1 ) > 31, 1, 0 )
NEXT
IF nCount < LEN( cCode ) / 2
lCodeA = .T.
cTemp = aCode[ 104 ]
nSum = 103
ELSE
cTemp = aCode[ 105 ]
nSum = 104
ENDIF
ENDIF
ELSE
IF cMode = "C"
lCodeC = .T.
cTemp = aCode[ 106 ]
nSum = 105
ELSEIF cMode = "A"
lCodeA = .T.
cTemp = aCode[ 104 ]
nSum = 103
ELSE
cTemp = aCode[ 105 ]
nSum = 104
ENDIF
ENDIF
nCount = 0
FOR n = 1 TO LEN( cCode )
nCount++
cCar = SUBSTR( cCode, n, 1 )
IF lCodeC
IF LEN( cCode ) = n
CTemp += aCode[ 101 ]
nSum += 100 * nCount
nCount++
nCar = ASC( cCar ) - 31
ELSE
nCar = VAL( SUBSTR( cCode, n, 2 ) ) + 1
n++
ENDIF
ELSEIF lCodeA
IF cCar > "_"
cTemp += aCode[ 101 ]
nCar = ASC( cCar ) - 31
ELSEIF cCar <= " "
nCar = ASC( cCar ) + 64
ELSE
nCar = ASC( cCar ) - 31
ENDIF
ELSE
IF cCar <= " "
cTemp += aCode[ 102 ]
nCar = ASC( cCar ) + 64
ELSE
nCar = ASC( cCar ) - 31
ENDIF
ENDIF
nSum += ( nCar - 1 ) * nCount
cTemp = cTemp + aCode[ nCar ]
NEXT
nSum = nSum % 103 + 1
cTemp = cTemp + aCode[ nSum ] + aCode[ 107 ]
cBar = ""
FOR n = 1 TO LEN( cTemp ) STEP 2
cBar += REPLICATE( "1", VAL( SUBSTR( cTemp, n, 1 ) ) )
cBar += REPLICATE( "0", VAL( SUBSTR( cTemp, n + 1, 1 ) ) )
NEXT
RETURN cBar