Convert CRC - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Convert CRC

light...light... Posts: 1Member
Please Can anyone convert this code from php to java?


<?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

Sign In or Register to comment.