EAN 13 encyclopedia

European Article Number (Abbreviation for European Article Code), wherein eAN-13, a total of 13 bits of code, is a relatively common bar code protocol and standard for general end products, which is mainly used in supermarkets and other retail industries. Therefore, it is relatively common for us. Pick up a supermarket product near you and you can see it on the package.

The code bits are 13 bits, among which the first 3 bits are the country and region codes (690-699 in mainland China), the middle 5-6 bits are the vendor identification codes (5 bits in the early stage, but now some new applications are 6 bits), and the next 4 bits (3 bits reserved for the 6-bit vendor identification codes) are the product codes, which are determined by the manufacturer. The last digit is the auto-generated verification code.

If the bar code is produced by itself, the first 12 bits can be produced randomly or according to their own rules, but the last bit check code must be calculated by statistical calculation formula.

Verification code production rules

The check code is calculated from the first 12 digits according to a certain formula. Its calculation formula is shown in the table below.

steps For example
① From left to right For example, 210213400150X (x is the parity code)
② Calculate the sum of the even digits 1 + 2 + 3 + 0 + 1 + 0 = 7
③ The sum of even digits * 3 7 times 3 is 21
④ Calculate the sum of the odd digits 2 + 0 + 1 + 4 + 5 = 0 + 12
⑤ The sum of even digits * 3 plus the sum of odd digits 21 plus 12 is 33
⑥ last bit of 10 – ⑤ (check bit 0 if 10) 10 minus 3 is 7

The last check bit is 7 2102134001507

EAN-13 code check bit calculation method, JS as an example

function  _EAN13(CodeString) {
      if (CodeString == "") return "";
      if (CodeString.length > 12) CodeString = CodeString.substring(0, 12);
      let Code = CodeString.split("");
      var A = 0;
      var B = 0;
      for (let i = 0; i < Code.length; i++) {
        if (i % 2 == 1) {
          A += parseInt(Code[i]);
        }
        else {
          B += parseInt(Code[i]);
        }
      }
      var C1 = B;
      var C2 = A * 3;
      var CC = (C1 + C2) % 10;
      var CheckCode = (10 - CC) % 10;
      return CodeString + CheckCode;
    }
Copy the code