# How do I calculate a Code 128 checksum?

Each character of the Code 128 character set has a numeric value. To calculate the checksum the values of all characters (including the start character) are multiplied by a weight factor, then summed up. The result is integer divided by 103, the remainder is the checksum (that's a basic Modulo 103 operation).

Here is a checksum function in C:

unsigned char calculateChecksum(unsigned char *data, int len)
{
int sum = *data - 32; // prime with start character
for(int i = 1;i<len;i++)
{
sum += i * indexFromCodeword(*(data + i));
}
return sum % 103 + 32;
}

The indexFromCodeword function simply gets the numeric value of the codeword from a table. See elsewhere in the FAQ for a Code 128 bit pattern table which can be used for that.

Code 128 is an alphanumeric barcode that encodes the full ASCII set of characters. Consequently the check digit can be a number, digit or special character, like blank or TAB. Therefore the check digit is usually not printed along with the human readable portion of Code 128. Also, most barcode scanners do not not transmit the check digit with the data read.