时间:2021-07-01 10:21:17 帮助过:19人阅读
public static int CRC16(final byte[] buffer) {
int crc = 0xffff;
for (int i = 0; i < buffer.length; i++) {
crc = ((crc >>> 8) | (crc << 8)) & 0xffff;
crc ^= (buffer[i] & 0xff);
crc ^= ((crc & 0xff) >> 4);
crc ^= (crc << 12) & 0xffff;
crc ^= ((crc & 0xff) << 5) & 0xffff;
}
crc &= 0xffff;
return crc;
}
function uRShift($a, $b)
{
if($b == 0) return $a;
return ($a >> $b) & ~(1<<(8*PHP_INT_SIZE-1)>>($b-1));
}
function crc16($buffer) {
$crc = 0xffff;
for ($i = 0; $i < strlen($buffer); $i++) {
$crc = ((uRShift($crc,8)) | ($crc << 8)) & 0xffff;
$crc ^= ($buffer[$i] & 0xff);
$crc ^= (($crc & 0xff) >> 4);
$crc ^= ($crc << 12) & 0xffff;
$crc ^= (($crc & 0xff) << 5) & 0xffff;
}
$crc &= 0xffff;
return $crc;
}