Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

BarreCode EAN encoding

I try to generate the encoding of the EAN13 barcode in order to obtain for the code 6181100327649 the following line 6BSLLAA*dchgej+
This to be able to use the EAN font and generate in my creative visuals.
Here is my code but it does not work :

function EAN(chaine) {
var i           = 0;
var first       = 0;
var checksum    = 0;
var CodeBarre   = "";
var tableA      = 0;

        if(chaine.match("/^\d{12}$/")){
                for (i = 1; i < 12; i += 2)
                {
                    chaine.substr(i, 1);
                    checksum += parseInt(chaine.substr(i, 1));
                }
                checksum *= 3;
                for (i = 0; i < 12; i += 2)
                {
                    checksum += parseInt(chaine.substr(i, 1));
                }

                chaine += (10 - checksum % 10) % 10;
                CodeBarre = chaine.substr(0, 1) + (chaine.fromCharCode(65 + parseInt(chaine.substr(1, 1)) ));
                first = parseInt(chaine.substr(0, 1));
                for (i = 2; i <= 6; i++)
                {
                    tableA = false;
                    switch (i)
                    {
                        case 2: if (first >= 0 && first <= 3) tableA = true; break;
                        case 3: if (first == 0 || first == 4 || first == 7 || first == 8) tableA = true; break;
                        case 4: if (first == 0 || first == 1 || first == 4 || first == 5 || first == 9) tableA = true; break;
                        case 5: if (first == 0 || first == 2 || first == 5 || first == 6 || first == 7) tableA = true; break;
                        case 6: if (first == 0 || first == 3 || first == 6 || first == 8 || first == 9) tableA = true; break;
                    }

                    if (tableA)
                        CodeBarre += (chaine.fromCharCode(65 + parseInt(chaine.substr(i, 1)) ));
                    else
                        CodeBarre += (chaine.fromCharCode(75 + parseInt(chaine.substr(i, 1)) ));
                }
                CodeBarre += "*";

                for (i = 7; i <= 12; i++)
                {
                    CodeBarre += (chaine.fromCharCode(97 + parseInt(chaine.substr(i, 1)) ));
                }
                CodeBarre += "+";
            }
            return CodeBarre;
        }
document.getElementById("demo").innerHTML = EAN("618110032764"); 

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There was mutliple issues here, but here is a working example for you

function getCharCode(chaine, index, position) {
  return String.fromCharCode(position + parseInt(chaine.substr(index, 1)));
}

function EAN(chaine) {
  var i = 0;
  var first = 0;
  var checksum = 0;
  var CodeBarre = "";
  var tableA = 0;

  if (chaine.match(/^\d{12}$/)) {
    for (i = 1; i < 12; i += 2) {
      chaine.substr(i, 1);
      checksum += parseInt(chaine.substr(i, 1));
    }
    checksum *= 3;
    for (i = 0; i < 12; i += 2) {
      checksum += parseInt(chaine.substr(i, 1));
    }

    chaine += (10 - checksum % 10) % 10;
    CodeBarre = chaine.substr(0, 1) + getCharCode(chaine, 1, 65);
    first = parseInt(chaine.substr(0, 1));
    for (i = 2; i <= 6; i++) {
      tableA = false;
      switch (i) {
        case 2:
          if (first >= 0 && first <= 3) tableA = true;
          break;
        case 3:
          if (first == 0 || first == 4 || first == 7 || first == 8) tableA = true;
          break;
        case 4:
          if (first == 0 || first == 1 || first == 4 || first == 5 || first == 9) tableA = true;
          break;
        case 5:
          if (first == 0 || first == 2 || first == 5 || first == 6 || first == 7) tableA = true;
          break;
        case 6:
          if (first == 0 || first == 3 || first == 6 || first == 8 || first == 9) tableA = true;
          break;
      }

      if (tableA)
        CodeBarre += getCharCode(chaine, i, 65); 
      else
        CodeBarre += getCharCode(chaine, i, 75); 
    }
    CodeBarre += "*";

    for (i = 7; i <= 12; i++) {
      CodeBarre += getCharCode(chaine, i, 97); 
    }
    CodeBarre += "+";
  }
  return CodeBarre;
}

document.getElementById("demo").innerHTML = EAN("618110032764");

Among those issues :

  • Regex shouldn’t be a string so no "" surrounding the expression
  • fromCharCode is a static method, should be used with String.fromCharCode
  • You should access characters from the string with the index on the loop

Here is the working demo: https://jsfiddle.net/t6r15qbd/1/

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading