
 Matti Anttonen - 2006-06-26 10:54:41 - 
In reply to message 1 from Matti Anttonen 
I found two more bugs from function _c128Encode.
Original code:
   $sum=0;
   $mfcStr="";
   if($useKeys=='C')
   {
      for($i=0;$i<strlen($barnumber);$i+=2)
      {
         $val=substr($barnumber,$i,2);
         if(is_int($val))
            $sum+=($i+1)*(int)($val);
         elseif($barnumber==chr(129))
            $sum+=($i+1)*100;
         elseif($barnumber==chr(130))
            $sum+=($i+1)*101;
         $mfcStr.=$encTable[$val];
      }
   }
Firstly: using is_int doesn't work properly on every case. You should use is_numeric instead.
Secondly: $sum for Code128 checksum is not count correctly. According the specification ( http://www.barcodeman.com/info/c128.php ) you should multiply $val with weights 1, 2, 3, ... , 27 but in your code you multiply $val with $i + 1, which get states 1, 3, 5, ... , 55.
I corrected these bugs with the fix below
Fixed code:
   $sum=0;
   $weight = 0;
   $mfcStr="";
   if($useKeys=='C')
   {
      for($i=0;$i<strlen($barnumber);$i+=2)
      {
         $weight++;
         $val=substr($barnumber,$i,2);
         if(is_numeric($val))
            $sum+=$weight*(int)($val);
         elseif($barnumber==chr(129))
            $sum+=$weight*100;
         elseif($barnumber==chr(130))
            $sum+=$weight*101;
         $mfcStr.=$encTable[(int) $val];
      }
   }
With the fixed code I managed to produce invoice codebars that were succesfully read with a codebar reader at local bank in Finland.