есть функция Код (Text): function crc16_kermit($string) { $crc = 0; for ( $x=0; $x<strlen( $string ); $x++ ) { $crc = $crc ^ ord( $string[$x] ); for ($y = 0; $y < 8; $y++) { if ( ($crc & 0x0001) == 0x0001 ) $crc = ( ($crc >> 1 ) ^ 0x1021 ); else $crc = $crc >> 1; } } $lb = ($crc & 0xff00) >> 8; $hb = ($crc & 0x00ff) << 8; $crc = $hb | $lb; //$crc = $lb | $hb; return $crc; } Входные данные hx ( 02030630 ) Тут ( https://www.lammertbies.nl/comm/info/crc-calculation.html) считает правильно, контрольная сумма получается 0x41B3, но при использовании функции выше, я не получаю такого результата. Так же проверял контрольную сумму здесь (http://crccalc.com/), тоже правильно. Алгоритм : CRC-16/KERMIT У меня получается 46615. Или это число нужно еще во что то перевести?
Тему к закрытию. Здесь ни кто не помог! Выкладываю рабочий класс - не жадный: PHP: <?php /** * @author Philip Burggraf <philip@pburggraf.de> */ abstract class AbstractCRC16 extends AbstractCRC { /** * @param string $buffer * * @return int */ public function calculate(string $buffer): int { $crc = $this->init; $bufferLength = strlen($buffer); for ($bufferPosition = 0; $bufferPosition < $bufferLength; ++$bufferPosition) { $character = ord($buffer[$bufferPosition]); if ($this->reverseIn) { $character = $this->binaryReverse($character, 8); } $crc = $this->lookupTable[(($crc >> 8) ^ $character) & 0xff] ^ ($crc << 8); $crc &= 0xffff; } if ($this->reverseOut) { $crc = $this->binaryReverse($crc, 16); } return $crc ^ $this->xorOut; } /** * @param int $polynomial * * @return array */ public function generateTable(int $polynomial): array { $tableSize = 256; $table = []; for ($iterator = 0; $iterator < $tableSize; ++$iterator) { $temp = 0; $a = ($iterator << 8); for ($j = 0; $j < 8; ++$j) { if ((($temp ^ $a) & 0x8000) !== 0) { $temp = (($temp << 1) ^ $polynomial); } else { $temp <<= 1; } $a <<= 1; } $table[$iterator] = $temp & 0xffff; } return $table; } } abstract class AbstractCRC { /** * @var int */ protected $poly; /** * @var int */ protected $init; /** * @var bool */ protected $reverseIn; /** * @var bool */ protected $reverseOut; /** * @var int */ protected $xorOut; /** * @var array */ protected $lookupTable; /** * @param string $buffer * * @return int */ abstract public function calculate(string $buffer): int; /** * @param int $polynomial * * @return array */ abstract protected function generateTable(int $polynomial): array; /** * @param int $binaryInput * @param int $bitlen * * @return int */ protected function binaryReverse(int $binaryInput, int $bitlen): int { $cloneBits = $binaryInput; $binaryInput = 0; $count = 0; while ($count < $bitlen) { $count++; $binaryInput <<= 1; $binaryInput |= ($cloneBits & 0x1); $cloneBits >>= 1; } return $binaryInput; } } class Kermit extends AbstractCRC16 { public function __construct() { $this->poly = 0x1021; $this->init = 0x0000; $this->reverseIn = true; $this->reverseOut = true; $this->xorOut = 0x0000; $this->lookupTable = $this->generateTable($this->poly); } } $crc16Kermit = new Kermit(); $str='02030630'; var_dump(dechex($crc16Kermit->calculate(hex2bin($str)))) ?>
@dimpase А можешь по подробнее рассказать что там в методах происходит? а то копировать с интернета как то страшно вдруг там вирус.