2009-07-16 3 views

Antwort

8
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; 
} 
+0

das ist falsch - sum1 sein sollte (Code [1] + Code [3] + Code [5]), sum2 sollte 3 * (Code [0] + Code [2] + Code [4] + code [6]) –

+0

OK. Nur mit EAN13 getestet. Scheint, dass Startgewichte in EAN8 und EAN13 unterschiedlich sind ... – SuperPro

+1

Ziffern werden von rechts gezählt, also weil 8 gerade und 13 ungerade ist, haben sie unterschiedliche Gewichte. Danke für die Bearbeitung, Downvote entfernt! –

11

Der Algorithmus ist in diesem wikipedia article on EAN behandelt, beachten Sie, dass EAN-8 auf die gleiche Weise wie EAN-13 berechnet wird.

Hier ist ein Beispielsystem von http://www.barcodeisland.com/ean8.phtml:

Angenommen wir die 7-stellige Meldung „5512345“ kodieren wollen, würden wir die Prüfsumme auf folgende Weise berechnen:

Barcode   5  5  1  2  3  4  5 
Odd/Even Pos? O  E  O  E  O  E  O 
Weighting  3  1  3  1  3  1  3 
Calculation 5*3 5*1 1*3 2*1 3*3 4*1 5*3 
Weighted Sum 15  5  3  2  9  4 15 

Das Ganze ist 15 + 5 + 3 + 2 + 9 + 4 + 15 = 53. 7 muss zu 53 addiert werden, um eine Zahl zu erzeugen, die durch 10 teilbar ist. Somit ist die Prüfsummenziffer 7 und der vollständige Strichcodewert ist "55123457".

string code="55123457"; 

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; 
5

Sorry für Wiedereröffnung

Java-Version

public int checkSum(String code){ 
     int val=0; 
     for(int i=0;i<code.length();i++){ 
      val+=((int)Integer.parseInt(code.charAt(i)+""))*((i%2==0)?1:3); 
     } 

     int checksum_digit = 10 - (val % 10); 
     if (checksum_digit == 10) checksum_digit = 0; 

     return checksum_digit; 
    } 
4

mit einer C# Version wieder erwacht:

public static bool IsValidEan13(string eanBarcode) 
    { 
     return IsValidEan(eanBarcode, 13); 
    } 

    public static bool IsValidEan12(string eanBarcode) 
    { 
     return IsValidEan(eanBarcode, 12); 
    } 

    public static bool IsValidEan14(string eanBarcode) 
    { 
     return IsValidEan(eanBarcode, 14); 
    } 

    public static bool IsValidEan8(string eanBarcode) 
    { 
     return IsValidEan(eanBarcode, 8); 
    } 

    private static bool IsValidEan(string eanBarcode, int length) 
    { 
     if (eanBarcode.Length != length) return false; 
     var allDigits = eanBarcode.Select(c => int.Parse(c.ToString(CultureInfo.InvariantCulture))).ToArray(); 
     var s = length%2 == 0 ? 3 : 1; 
     var s2 = s == 3 ? 1 : 3; 
     return allDigits.Last() == (10 - (allDigits.Take(length-1).Select((c, ci) => c*(ci%2 == 0 ? s : s2)).Sum()%10))%10; 
    } 
1

Hier ist die Java-Version für EAN13

private int calcChecksum(String first12digits) { 
    char[] char12digits = first12digits.toCharArray(); 
    int[] ean13 = {1,3}; 
    int sum = 0; 
    for(int i = 0 ; i<char12digits.length; i++){ 
     sum += Character.getNumericValue(char12digits[i]) * ean13[i%2]; 
    } 

    int checksum = 10 - sum%10; 
    if(checksum == 10){ 
     checksum = 0; 
    } 

    return checksum; 
} 
1
class GTIN(object): 

    def __init__(self, barcode=''): 
     self.barcode = barcode 

    def __checkDigit(self, digits): 
      total = sum(digits) + sum(map(lambda d: d*2, digits[-1::-2])) 
      return (10 - (total % 10)) % 10 

    def validateCheckDigit(self, barcode=''): 
     barcode = (barcode if barcode else self.barcode) 
     if len(barcode) in (8,12,13,14) and barcode.isdigit(): 
      digits = map(int, barcode) 
      checkDigit = self.__checkDigit(digits[0:-1]) 
      return checkDigit == digits[-1] 
     return False 

    def addCheckDigit(self, barcode=''): 
     barcode = (barcode if barcode else self.barcode) 
     if len(barcode) in (7,11,12,13) and barcode.isdigit(): 
      digits = map(int, barcode) 
      return barcode + str(self.__checkDigit(digits)) 
     return '' 
2

Hier ist eine MySQL-Version für EAN13:

 
    SET @first12digits="123456789012"; 
    SELECT @first12digits, 
IF (
    (@check:=10-MOD(
     (CAST(SUBSTRING(@first12digits, 1, 1) AS DECIMAL))+ 
     (CAST(SUBSTRING(@first12digits, 2, 1) AS DECIMAL) * 3)+ 
     (CAST(SUBSTRING(@first12digits, 3, 1) AS DECIMAL))+ 
     (CAST(SUBSTRING(@first12digits, 4, 1) AS DECIMAL) * 3)+ 
     (CAST(SUBSTRING(@first12digits, 5, 1) AS DECIMAL))+ 
     (CAST(SUBSTRING(@first12digits, 6, 1) AS DECIMAL) * 3)+ 
     (CAST(SUBSTRING(@first12digits, 7, 1) AS DECIMAL))+ 
     (CAST(SUBSTRING(@first12digits, 8, 1) AS DECIMAL) * 3)+ 
     (CAST(SUBSTRING(@first12digits, 9, 1) AS DECIMAL))+ 
     (CAST(SUBSTRING(@first12digits, 10, 1) AS DECIMAL) * 3)+ 
     (CAST(SUBSTRING(@first12digits, 11, 1) AS DECIMAL))+ 
     (CAST(SUBSTRING(@first12digits, 12, 1) AS DECIMAL) * 3) 
    ,10)) = 10, 0, @check 
    ) AS checkDigit; 

Es war ein Fehler. Wenn Calc Ergebnis = 10, dann überprüfen Sie die Ziffer = 0.
Hier unten eine bessere Version für EAN14.



    SET @first13digits="1234567890123"; 
    SELECT @txCode13:[email protected], 
     @iCheck := (
      10 - (
      (
       MID(@txCode13, 2, 1) + 
       MID(@txCode13, 4, 1) + 
       MID(@txCode13, 6, 1) + 
       MID(@txCode13, 8, 1) + 
       MID(@txCode13, 10, 1) + 
       MID(@txCode13, 12, 1) 
      ) + (
       MID(@txCode13, 1, 1) + 
       MID(@txCode13, 3, 1) + 
       MID(@txCode13, 5, 1) + 
       MID(@txCode13, 7, 1) + 
       MID(@txCode13, 9, 1) + 
       MID(@txCode13, 11, 1) + 
       MID(@txCode13, 13, 1) 
       ) * 3) % 10 
      ) AS iCheck, 
     @iCheckDigit := IF(@iCheck = 10, 0, @iCheck) AS checkDigit, 
     CONCAT(@t 

xCode13, CAST(@iCheckDigit AS CHAR)) AS EAN14WithCheck 
+0

Es gibt einen Fehler.Wenn das Berechnungsergebnis = 10 ist, ist die Prüfziffer 0. –

0

Java Version:

Es funktioniert perfekt

public static int checkSum(String code){ 
     int val=0; 
     for(int i=0; i<code.length()-1; i++){ 
      val+=((int)Integer.parseInt(code.charAt(i)+""))*((i%2==0)?1:3); 
     } 

     int checksum_digit = (10 - (val % 10)) % 10; 

     return checksum_digit; 
    } 
0

Python EAN13 Berechnung Prüfzifferalgorithmus basierend auf Najoua Mahi Java-Funktion:

def generateEAN13CheckDigit(self, first12digits): 
    charList = [char for char in first12digits] 
    ean13 = [1,3] 
    total = 0 
    for order, char in enumerate(charList): 
     total += int(char) * ean13[order % 2] 

    checkDigit = 10 - total % 10 
    if (checkDigit == 10): 
     return 0 
    return checkDigit 
0

Dieses sowohl EAN arbeitet 13 und EAN8:

public static String generateEAN(String barcode) { 
    int first = 0; 
    int second = 0; 

    if(barcode.length() == 7 || barcode.length() == 12) { 

     for (int counter = 0; counter < barcode.length() - 1; counter++) { 
      first = (first + Integer.valueOf(barcode.substring(counter, counter + 1))); 
      counter++; 
      second = (second + Integer.valueOf(barcode.substring(counter, counter + 1))); 
     } 
     second = second * 3; 
     int total = second + first; 
     int roundedNum = Math.round((total + 9)/10 * 10); 

     barcode = barcode + String.valueOf(roundedNum - total); 
    } 
    return barcode; 
} 
0

Heute brauche ich eine PHP-Version, ich erinnere mich über diese Seite und kopieren Sie von der Java-Version. Vielen Dank.

function getEAN13($txEan12) 
    { 
    $iVal=0; 

    for($i=0; $i<strlen($txEan12); $i++) 
     { 
     $iSingleCharVal = intval(substr($txEan12, $i, 1));  // extract value of one char 
     $iSingleCharMult = $iSingleCharVal * ($i%2==0 ? 1 : 3); // calculate depending from position 
     $iVal+= $iSingleCharMult; // sum 
     } 

    $iCheckDigit = 10 - ($iVal % 10); 
    if ($iCheckDigit == 10) $iCheckDigit = 0; 
    return $txEan12 . $iCheckDigit; 
    }