note 39545 added to function.crc32

From: Date: Sun, 01 Feb 2004 22:27:27 +0000
Subject: note 39545 added to function.crc32
Groups: php.notes 
Request: Send a blank email to php-notes+get-64523@lists.php.net to get a copy of this message
bit by bit crc32 computation <?php function bitbybit_crc32($str,$first_call=false){ //reflection in 32 bits of crc32 polynomial 0x04C11DB7 $poly_reflected=0xEDB88320; //=0xFFFFFFFF; //keep track of register value after each call static $reg=0xFFFFFFFF; //initialize register on first call if($first_call) $reg=0xFFFFFFFF; $n=strlen($str); $zeros=$n<4 ? $n : 4; //xor first $zeros=min(4,strlen($str)) bytes into the register for($i=0;$i<$zeros;$i++) $reg^=ord($str{$i})<<$i*8; //now for the rest of the string for($i=4;$i<$n;$i++){ $next_char=ord($str{$i}); for($j=0;$j<8;$j++) $reg=(($reg>>1&0x7FFFFFFF)|($next_char>>$j&1)<<0x1F) ^($reg&1)*$poly_reflected; } //put in enough zeros at the end for($i=0;$i<$zeros*8;$i++) $reg=($reg>>1&0x7FFFFFFF)^($reg&1)*$poly_reflected; //xor the register with 0xFFFFFFFF return ~$reg; } $str="123456789"; //whatever $blocksize=4; //whatever for($i=0;$i<strlen($str);$i+=$blocksize) $crc=bitbybit_crc32(substr($str,$i,$blocksize),!$i); ?> ---- Manual Page -- http://www.php.net/manual/en/function.crc32.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+39545 Delete -- http://master.php.net/manage/user-notes.php?action=delete+39545&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+39545&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#64523) next »