PHP Classes

Fix for Code128 C

Recommend this page to a friend!

      Barcode  >  All threads  >  Fix for Code128 C  >  (Un) Subscribe thread alerts  
Subject:Fix for Code128 C
Summary:Fix for a problem printing Code128 C type barcodes correctly.
Messages:2
Author:Matti Anttonen
Date:2006-06-20 11:25:19
Update:2006-06-26 10:54:41
 

  1. Fix for Code128 C   Reply   Report abuse  
Picture of Matti Anttonen Matti Anttonen - 2006-06-20 11:25:19
I was printing Code128 C type codebars when I found out that all the bars weren't printed. I found that the bug (at least with my settings) was in barcode.inc.php's _c128Encode function which didn't automatically rip of the other zero in 00-09 cases before indexing an array.

To fix the bug I made one additional cast to integer:

Original line: $mfcStr.=$encTable[$val];
Fixed line: $mfcStr.=$encTable[(int) $val];

After the fix the amount of bars returned to normal.

  2. Re: Fix for Code128 C   Reply   Report abuse  
Picture of Matti Anttonen 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.