How can I checksum a Code128 barcode in C++?

Asked

Viewed 61 times

0

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;
}
  • This site was made in Portuguese and does not accept questions and answers in English.

  • I’m sorry, I’m new at this. How do I remove the question from the English version to publish in English (the reason is because I have some urgency in the answer and I assume that in English I may have more answers)?

  • You can copy all the text and paste it into http://stackoverflow.com and/or edit the same question and rewrite it in English (instead of simply removing it).

  • I appreciate the quick response. So I’ll copy it to the English version. How do I remove this version so it doesn’t get duplicated?

  • I honestly don’t know. I suggest looking in the FAQ.

  • 1

    You do not need to remove this question or open another one. You can [Edit] to change the content to English. :)

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.