How to calculate a checkdigit for a gs1-128 SSCC barcode

201 views Asked by At

Im looking for a method to calculate checkdigits for SSCC barcodes that are compliant with GS1.org. https://www.gs1.org/services/how-calculate-check-digit-manually

The SSSCC barcode consists of 2 parts. GTIN nr for example: 1234567 Indentifier : 123456789

I can't find any library that calculates the check digit so far. for example

GTIN = '1234567'
Identifier = '123456789'
raw_sscc = GTIN + Identifier
SSCC_code = ADD_CHECKDIGIT_SSCC(raw_sscc)
2

There are 2 answers

0
BugHunter On

You can calculate SSCC check digits following the GS1 specifications for example.

def calculate_ssc_check_digit(gtin, identifier):
    ssc = gtin + identifier
    total = 0
    for i, char in enumerate(reversed(ssc)):
        if i % 2 == 0:
            total += int(char)
        else:
            total += 3 * int(char)
    check_digit = (10 - (total % 10)) % 10
    return check_digit

gtin = '1234567'
identifier = '123456789'
check_digit = calculate_ssc_check_digit(gtin, identifier)
sscc = gtin + identifier + str(check_digit)
print("Check Digit:", check_digit)
print("SSCC:", sscc)
0
Shwans On

Thank you for your reply. The function did not give the correct result. The iteration starts with zero this caused the calculation to be incorrect. A made small changes and all my tests so far gave a correct result.

def calculate_ssc_check_digit(gtin_identifier):
    gi = gtin_identifier
    total = 0
    for i, char in enumerate(reversed(gi)):
        if ((i+1) % 2) == 0:
            total += int(char)
        else:
            total += 3 * int(char)
    check_digit = (10 - (total % 10)) % 10
    return check_digit
   
gtin_indentifier = '12365481212125215'
check_digit = calculate_ssc_check_digit(gtin_indentifier)
sscc = gtin_indentifier + str(check_digit)
print("Check Digit:", check_digit)
print("SSCC:", sscc)