PHP CRC not the same as Java CRC -
i'm trying crc16 in php , same results in java.
some criteria crc16
- polynomial of 8005
- initial remainder of 0
- a final xor value of 0
- neither input nor output reflected
in java pass 5 byte message containing values 1,2,3,4,5 , value 0xb059.
java code
package crc; public class crc { public static void main(string[] args) { char crc = 0x0000; char temp = 0x0000; char[] data = new char[5]; data[0]=1;data[1]=2;data[2]=3;data[3]=4;data[4]=5; for(int i=0; i<data.length; i++){ char value = (char)data[i]; temp =(char)(value ^(char)(crc >> 8)); temp =(char)(temp ^(char)(temp >> 4)); temp =(char)(temp ^(char)(temp >> 2)); temp =(char)(temp ^(char)(temp >> 1)); crc = (char)((char)(crc << 8) ^ (char)(temp<<15)^(char)(temp << 2)^temp); } system.out.println(crc); } }
php code
function crc16($str){ $crc = 0x0000; $temp = 0x0000; $length = strlen($str); $data = unpack("c*", $str); for($i=0;$i<($length);$i++){ $value = $data[$i]; $temp = ($value ^($crc >> 8)); $temp = ($temp ^($temp >> 4)); $temp = ($temp ^($temp >> 2)); $temp = ($temp ^($temp >> 1)); $crc = (($crc << 8) ^ ($temp<<15)^($temp << 2)^$temp); } return $crc; } echo crc16("\x01\x02\x03\x04\x05");
took code , converted php , no luck.if has direction great.
thanks
Comments
Post a Comment