ATOUTFOX
COMMUNAUTÉ FRANCOPHONE DES PROFESSIONNELS FOXPRO
Visual FoxPro : le développement durable

Calcul et verification d'un code EAN   



L'auteur

Gregory Adam
Belgique Belgique
Membre Actif (personne physique)
# 0000001121
enregistré le 04/06/2006

Fiche personnelle


Note des membres
pas de note

Contributions > 01 - PRG : Programmation > Calculs de clefs et Checksums (rib, iban, ...)

Calcul et verification d'un code EAN
# 0000000702
ajouté le 06/06/2009 12:27:33 et modifié le 06/06/2009
consulté 10183 fois
Niveau débutant


Le téléchargement des pièces jointes est limité aux membres
Veuillez vous identifier ou vous inscrire si vous n'avez pas encore de compte ...
Description

http://en.wikipedia.org/wiki/European_Article_Number

 

Calcul et verification de codes EAN - 8, 13, 18, ...

Pas de verification de la longueur de la clef

 

Code source :
function Do_it()

  local s
text to s noshow
73513537
3356690056900 335669005690*
4078500139687 407850013968*
4078500156998 407850015699*
4078500258760 407850025876*
4078500407850 4078500407854
4078500407850 4078500407854
4078500407851 4078500407854
4078500407851 4078500407854
4078500297684 407850029768*
4078500297783 407850029778*
4078500966825 407850096682*
4078500779500 407850077950*
4078500779609 407850077960*
4078500779708 407850077970*
4078500779807 407850077980*
4078500779906 407850077990*
4078500788700 407850078870*
4078500788809 407850078880*
4078500788908 407850078890*
4078500798709 407850079870*
4078500799300 407850079930*
4078500799508 407850079950*
4078500816663 407850081666*
4078500407853 4078500407854
4078500816762 407850081676*
4078500407853 4078500407854
4078500816861 407850081686*
4078500407853 4078500407854
endtext


  local i, aa[1], naa, CodeIn, CodeOut

  naa = alines(aa, m.s)

  for i = 1 to m.naa

    CodeIn = getwordnum(aa[m.i], 1)
    ? m.CodeIn

    if( ean_Check(m.CodeIn, @m.CodeOut) )
      ?? ' OK'
    else
      ?? ' Fail >> ', m.CodeOut
    endif

  endfor

endfunc
*---------------------------------------------------------------------------
&& CodeIn must have a check digit
&& if ean_Check() returns false, CodeOut contains the right code
&& source : http://en.wikipedia.org/wiki/European_Article_Number

#define  nChar_0        0x30
#define  nChar_9        0x39

function ean_Check(CodeIn, CodeOut)


  local i, n, Weight[2], WeightIndex, c
  Weight[1] = 1
  Weight[2] = 3
  WeightIndex = 2  && right align

  n = 0
  for i = len(m.CodeIn) - 1 to 1 step -1
    n = m.n + (asc(substr(m.CodeIn, m.i,1)) - nChar_0) * m.Weight[m.WeightIndex]
    WeightIndex = mod(m.WeightIndex ,2) + 1
  endfor

  c = chr(mod(10 - mod(m.n, 10), 10) + nChar_0)

  ifright(m.CodeIn,1) == m.c )
    return TRUE
  else
    CodeOut = stuff(m.CodeIn, len(m.CodeIn), 1, m.c)
    return FALSE

  endif

endfunc
*---------------------------------------------------------------------------
Commentaires
Aucun commentaire enregistré ...

Publicité

Les pubs en cours :


www.atoutfox.org - Site de la Communauté Francophone des Professionnels FoxPro - v3.4.0 - © 2004-2024.
Cette page est générée par un composant COM+ développé en Visual FoxPro 9.0-SP2-HF3