/** * getCode128BarCode * * This function generates Code128 barcode for the given string. It uses subsets B and C when generating barcode * string (not subset A). * @param In &strDataToFormat - string to format into the barcode. * Returns: ASCII characters needed to represent the barcode. * * ASSUMPTIONS: The &strDataToFormat string does not contain any invalid characters. Only ASCII characters 32 to 126 are * allowed - if there are invalid characters, an empty string is returned. The function itself appends the necessary special * ASCII characters to generate the barcode. * * The algorithm for creating barcodes is based upon the sample code provided on this site, with slight modifications: * http://grandzebu.net/index.php?page=/informatique/codbar-en/code128.htm **/ Function getCode128BarCode(&strDataToFormat As string) Returns string Local boolean &boolSubsetB = True; Local boolean &boolIsDigits; Local string &strBarcode = ""; Local integer &i; Local integer &intCharNum, &charsToCheck, &intCheckSum; /* No need to process an empty string - simply return what was put in */ If Len(&strDataToFormat) = 0 Then Return &strBarcode; End-If; /* Check for valid characters */ For &i = 1 To Len(&strDataToFormat) &intCharNum = Code(Substring(&strDataToFormat, &i, 1)); If &intCharNum >= 32 And &intCharNum <= 126 Then Else /* invalid characters found - return empty string */ Return ""; End-If; End-For; &i = 1; While &i <= Len(&strDataToFormat) If &boolSubsetB Then /* see if it's worth it to switch to subset C */ /* yes for 4 digits at start or end, else if 6 digits */ If (&i = 1) Or (&i + 3) = Len(&strDataToFormat) Then &charsToCheck = 4; Else &charsToCheck = 6; End-If; &boolIsDigits = False; If IsDigits(Substring(&strDataToFormat, &i, &charsToCheck)) And (Len(Substring(&strDataToFormat, &i, &charsToCheck)) = &charsToCheck) Then &boolIsDigits = True; End-If; If &boolIsDigits Then If &i = 1 Then /* Put Start C character, to indicate barcode starts with subset C */ &strBarcode = Char(210); Else /* Put Code C character, to indicate switch to subset C */ &strBarcode = &strBarcode | Char(204); End-If; &boolSubsetB = False; Else If &i = 1 Then /* Put Start B character, to indicate barcode starts with subset B */ &strBarcode = Char(209); End-If; End-If; End-If; If Not &boolSubsetB Then /* We are on subset C, try to process 2 digits */ &boolIsDigits = False; If IsDigits(Substring(&strDataToFormat, &i, 2)) And (Len(Substring(&strDataToFormat, &i, 2)) = 2) Then &boolIsDigits = True; End-If; If &boolIsDigits Then /* 2 digits - process using subset C */ &intCharNum = Value(Substring(&strDataToFormat, &i, 2)); If &intCharNum = 0 Then &intCharNum = 212; Else If &intCharNum < 95 Then &intCharNum = &intCharNum + 32; Else &intCharNum = &intCharNum + 105; End-If; End-If; &strBarcode = &strBarcode | Char(&intCharNum); &i = &i + 2; Else /* revert back to subset B */ &strBarcode = &strBarcode | Char(205); &boolSubsetB = True; End-If; End-If; If &boolSubsetB Then /* Process 1 digit with subset B */ &strBarcode = &strBarcode | Substring(&strDataToFormat, &i, 1); &i = &i + 1 End-If; End-While; /* Calculation of the checksum */ For &i = 1 To Len(&strBarcode) &intCharNum = Code(Substring(&strBarcode, &i, 1)); If &intCharNum = 212 Then &intCharNum = 0; Else If &intCharNum < 127 Then &intCharNum = &intCharNum - 32; Else &intCharNum = &intCharNum - 105; End-If; End-If; If &i = 1 Then &intCheckSum = &intCharNum; Else &intCheckSum = &intCheckSum + (&intCharNum * (&i - 1)); End-If; End-For; /* Apply modulo 103 to the weighted total to get the checksum */ &intCheckSum = Mod(&intCheckSum, 103); /* Calculate checksum ASCII code */ If &intCheckSum < 95 Then &intCheckSum = &intCheckSum + 32; Else &intCheckSum = &intCheckSum + 105; End-If; /* Add the checksum and the STOP */ &strBarcode = &strBarcode | Char(&intCheckSum) | Char(211); Return &strBarcode; End-Function;