How does this CRC 16 Java code translate into PHP's

Source: Internet
Author: User
Tags unpack
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.