How does this CRC 16 Java code translate into PHP's
This post was last edited by Oelenson on 2014-06-21 12:49:27
How this CRC 16 Java code conversion to PHP, their own PHP to the code is not
$str 2= ' Aqebaqeb ';
$str 2=base64_decode ($str 2);
Java code
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 <<) & 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 <<) & 0xFFFF;
$CRC ^= (($CRC & 0xff) << 5) & 0xFFFF;
}
$CRC &= 0xFFFF;
return $CRC;
}
------Solution--------------------
Java >>> (left 0 right shift) does not require special processing in PHP, can be directly >>
Translate (CRC >>> 8) to (($CRC >> 8) & 0xff)
Java byte[] type, no corresponding PHP data type
You can take the inner code byte by bit ($buffer [$i])
You can also enter a function $buffer = array_values (Unpack (' C ', $buffer));
Unpack (' C ', $buffer) returns an array that starts with subscript 1, so array_values to sort it out.
If you use a Foreach loop, or the For loop starts at 1, you don't need to defragment the
Everything else should be fine.
If you feel that you still have a problem, give a few sets of results from Java running