从 EAN-13 条码计算 GTIN 的库

Library for calculating GTIN from EAN-13 barcode

我正在从应该被馈送到 GDSN 数据池的后端创建一个 XML。该公司有一个非常老旧的后端,每个项目只有自己的 PLU 编号和条形码。我所知道的是(至少在冰岛)大多数 GTIN 是 EAN-13 条形码,前面有一个填充的 0,尽管情况并非总是如此。您是否知道可以检查 GTIN 是否正确的图书馆,即计算校验位?

我正在使用 windows 表单并使用 C#。

首先验证你想做什么: https://www.gs1.org/services/check-digit-calculator

GTIN 有 2 种可能,因为它必须是 14 位数字。

  • 你描述的情况,那你在左边补一个0
  • 直接提供正确长度的GTIN(这是可能的,左边的数字不会是0)

根据您知道 gtin 字符串仅包含数字的事实,这是一个关于如何检查这一点的简单示例:

    public Boolean ValidateGTIN(string gtin)
    {
        string tmpGTIN = gtin;
        if (tmpGTIN.Length < 13)
        {
            Console.Write("GTIN code is invalid (should be at least 13 digits long)");
            return false;
        }
        else if (tmpGTIN.Length == 13)
        {
            tmpGTIN = "0" + gtin;
        }
        // Now that you have a GTIN with 14 digits, you can check the checksum
        Boolean IsValid = false;
        int Sum = 0;
        int EvenSum = 0;
        int CurrentDigit = 0;
        for (int pos = 0; pos <= 12; ++pos)
        {                
            Int32.TryParse(tmpGTIN[pos].ToString(), out CurrentDigit);
            if (pos % 2 == 0)
            {                    
                EvenSum += CurrentDigit;
            }                    
            else
            {
                Sum += CurrentDigit;
            }                    
        }
        Sum += 3 * EvenSum;
        Int32.TryParse(tmpGTIN[13].ToString(), out CurrentDigit);
        IsValid = ((10 - (Sum % 10)) % 10) == CurrentDigit;
        if (!IsValid)
        {
            Console.Write("GTIN code is invalid (wrong checksum)");
        }
        return IsValid;
    }

谢谢。这差不多了。我想更进一步 - 我将复制您的代码并添加一点:

//First create a function only for validating
//This is your code to almost all - variable names change  
public Boolean validateGTIN(string gtin)
    {
        Boolean IsValid = false;
        int Sum = 0;
        int EvenSum = 0;
        int CurrentDigit = 0;
        for (int pos = 0; pos <= 12; ++pos)
        {                
            Int32.TryParse(gtin[pos].ToString(), out CurrentDigit);
            if (pos % 2 == 0)
            {                    
                EvenSum += CurrentDigit;
            }                    
            else
            {
                Sum += CurrentDigit;
            }                    
        }
        Sum += 3 * EvenSum;
        Int32.TryParse(GTIN[13].ToString(), out CurrentDigit);
        IsValid = ((10 - (Sum % 10)) % 10) == CurrentDigit;
        if (!IsValid)
        {
            Console.Write("GTIN code is invalid (wrong checksum)");
        }
        return IsValid;
    }
//Here we change quite a bit to accommodate for edge cases:
//We return a string which is the GTIN fully formed or we throw and exception.
public String createGTIN(string bcFromBackend)
    {
        string barcodeStr = bcFromBackend;
//Here we check if the barcode supplied has fewer than 13 digits
        if (barcodeStr.Length < 13)
        {
            throw new System.ArgumentException("Barcode not an EAN-13 
            barcode");
        }
        //If the barcode is of length 13 we start feeding the value with a padded 0 
        //into our validate fuction if it returns false then we pad with 1 and so on
        //until we get to 9. It then throws an error if not valid  
        else if (barcodeStr.Length == 13)
        {
            if(validateGTIN("0"+ barcodeStr))
            {
                 return "0" + barcodeStr;
            } 
            else if(validateGTIN("1" + barcodeStr))
            {
                 return "1" + barcodeStr;
            }
            else if(validateGTIN("2" + barcodeStr))
            {
                 return "2" + barcodeStr;
            }  
            else if(validateGTIN("3" + barcodeStr))
            {
                 return "3" + barcodeStr;
            }
            else if(validateGTIN("4" + barcodeStr))
            {
                 return "4" + barcodeStr;
            }
            else if(validateGTIN("4" + barcodeStr))
            {
                 return "4" + barcodeStr;
            }
            else if(validateGTIN("5" + barcodeStr))
            {
                 return "5" + barcodeStr;
            }
            else if(validateGTIN("6" + barcodeStr))
            {
                 return "6" + barcodeStr;
            }
            else if(validateGTIN("7" + barcodeStr))
            {
                 return "7" + barcodeStr;
            }
            else if(validateGTIN("8" + barcodeStr))
            {
                 return "8" + barcodeStr;
            }
            else if(validateGTIN("9" + barcodeStr))
            {
                 return "9" + barcodeStr;
            } else {
                 throw new System.InvalidOperationException("Unable to create valid 
                 GTIN from given barcode")
            }
        }
        //Lastly if the barcode is of length 14 we try with this value. Else throw 
        //error
        else if(barcodeStr.Length == 14)
        {
            if(validateGTIN(barcodeStr)
            {
                 return barcodeStr;
            }
            else
            {
                 throw new System.InvalidOperationException("Unable to create valid 
                 GTIN from given barcode");
            }
        }

希望这是有道理的。我没有通过测试发送代码,因为我没有在我当前的计算机上安装 IDE。是