# DHL用自己的check digits规范把我坑了一下

In the UPC-A system, the check digit is calculated as follows:

Add the digits in the odd-numbered positions (first, third, fifth,
etc.) together and multiply by three. Add the digits in the
even-numbered positions (second, fourth, sixth, etc.) to the result.
Find the result modulo 10 (i.e. the remainder when divided by 10… 10
goes into 58 5 times with 8 leftover). If the result is not zero,
subtract the result from ten.

Before the actual pairs there is a start code consisting of nnnn
(narrow bar-narrow space- narrow bar-narrow space), and after all
symbols there is the stop code consisting of Wnn (Wide bar-narrow
space-narrow bar).

Because digits are encoded by pairs, only an even number of digits can
be encoded. Typically an odd number of digits is encoded by adding a
"0" as first digit, but sometimes an odd number of digits is encoded
by using five narrow spaces in the last digit.

static public function compute($code,$crc, $type){ if (!$crc) {
if (strlen($code) % 2)$code = '0' . $code; } else { if ( ($type == 'int25') && (strlen($code) % 2 == 0) )$code = '0' . $code;$odd = true;
$sum = 0; for($i=strlen($code)-1;$i>-1; $i--){$v = intval($code[$i]);
$sum +=$odd ? 3 * $v :$v;
$odd = !$odd;
}
$code .= (string) ((10 -$sum % 10) % 10);
}
return($code); }  然而不传递checkdigit进去生成出来的依旧不对。 这时候想到DHL一开始发的Check digit calculation , Factor 4 and 9 see Specification。 wikipedia了一下。终于发现了 Identcode and Leitcode are variants of interleaved 2 of 5 with check digits used by Deutsche Post. 一开始还不知道Deutsche Post是什么。查了一下 ，不就是DHL么。。。。 好坑啊。DHL还用自己的checkdigit规范。 果断去把生成类里的代码改成 $sum += $odd ? 4 *$v : 9 * \$v;


• 全部
• 标签