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; }
Reply to discussion (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
$str 2= ' Aqebaqeb ';
$str 2=base64_decode ($str 2);
Java Gets the result 8357
PHP gets 33653 results.
> 8) & 0xff) | ($CRC << 8)) & 0xFFFF, $CRC ^= (Ord ($buffer [$i]) & 0xff), $CRC ^= (($CRC & 0xFF) >> 4), $CRC ^= ($CRC <<) & 0xFFFF, $CRC ^= (($CRC & 0xff) << 5) & 0xFFFF; $CRC &= 0xffff;return $CRC;} $str =base64_decode (' aqebaqebdym= '); $str 2= ' Aqebaqeb '; $str 2=base64_decode ($str 2); $arr 2=unpack (' h* ', $str 2); $arr Unpack (' h* ', $str);p Rint_r ($arr); Echo '-----------------------------------'; Echo crc16 ($str 2);
Why $str 2=base64_decode ($str 2);
Has base64 been encoded in Java?
Let you give more groups, why only give a group?
Thank you, it's done.