Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EAN 8 : How to calculate checksum digit?

I need to create EAN 8 bar code programmatically. I search an algorithm to calculate the checksum digit.

like image 931
Ben Avatar asked Jul 16 '09 10:07

Ben


1 Answers

int checkSum(const std::vector<int>& code) const
{
    if (code.size() < 8) return false;

    for( SIZE_T i = 0; i< code.size(); i++ )
    {
        if( code[i] < 0 ) return false;
    }

    int sum1 = code[1] + code[3] + code[5] 
    int sum2 = 3 * (code[0] + code[2] + code[4] + code[6]);

    int checksum_value = sum1 + sum2;
    int checksum_digit = 10 - (checksum_value % 10);
    if (checksum_digit == 10) checksum_digit = 0;

    return checksum_digit;
}
like image 151
SuperPro Avatar answered Sep 21 '22 14:09

SuperPro