I compared my results with others , and it looks like my checker digit is incorrect. Here is my function:
QString Barcode::StringToBarcode_Code128(const QString &value){
QString returnValue = QString();
const int length = value.length();
if (length > 0){
bool isValid = true;
int currentChar;
// Check for valid characters
for (int charCount = 0; charCount < length; charCount++){
currentChar = (int)value.at(charCount).toLatin1();
if (!(currentChar >= 32 && currentChar <= 126)){
isValid = false;
break;
}
}
// Barcode is full of ascii characters, we can now process it
if (isValid){
bool isTableB = true;
int charPos = 0;
int minCharPos;
while (charPos < length){
if (isTableB){
// See if interesting to switch to table C
// yes for 4 digits at start or end, else if 6 digits
if (charPos == 0 || charPos + 4 == length)
minCharPos = 4;
else
minCharPos = 6;
minCharPos = IsNumber(value, charPos, minCharPos);
if (minCharPos < 0){
// Choice table C
if (charPos == 0){
// Starting with table C
returnValue = (char)205;
}
else{
// Switch to table C
returnValue = returnValue + (char)199;
}
isTableB = false;
}
else{
if (charPos == 0){
// Starting with table B
returnValue = (char)204;
}
}
}
if (!isTableB){
// We are on table C, try to process 2 digits
minCharPos = 2;
minCharPos = IsNumber(value, charPos, minCharPos);
if (minCharPos < 0) // OK for 2 digits, process it
{
currentChar = (int)value.midRef(charPos, 2).toString().toStdString().c_str();
currentChar = currentChar < 95 ? currentChar + 32 : currentChar + 100;
returnValue = returnValue + (char)currentChar;
charPos += 2;
}
else{
// We haven't 2 digits, switch to table B
returnValue = returnValue + (char)200;
isTableB = true;
}
}
if (isTableB){
// Process 1 digit with table B
returnValue = returnValue + value.at(charPos);
charPos++;
}
}
// Calculation of the checksum
int checksum = 0;
for (int loop = 0; loop < returnValue.length(); loop++){
currentChar = (int)returnValue.at(loop).toLatin1();
currentChar = currentChar < 127 ? currentChar - 32 : currentChar - 100;
if (loop == 0)
checksum = currentChar;
else{
checksum = (checksum + (loop * currentChar)) % 103;
}
}
// Calculation of the checksum ASCII code
checksum = checksum < 95 ? checksum + 32 : checksum + 100;
// Add the checksum and the STOP
qDebug() << value << ":: " << (char)checksum;
returnValue = returnValue +
(char)checksum +
(char)206;
}
}
return returnValue;
}
int Barcode::IsNumber(const QString &InputValue, int CharPos, int MinCharPos){
// if the MinCharPos characters from CharPos are numeric, then MinCharPos = -1
MinCharPos--;
if (CharPos + MinCharPos < InputValue.length()){
while (MinCharPos >= 0){
if ((int)InputValue.at(CharPos + MinCharPos).toLatin1() < 48
|| (int)InputValue.at(CharPos + MinCharPos).toLatin1() > 57){
break;
}
MinCharPos--;
}
}
return MinCharPos;
}