CRC Calculator (convert C to PHP)
Hi :)
I need to calculate the CRC for a large number. I found some code written in C and need to translate it to PHP, but I think I'm failing at that cause I don't get some parts of the code.
I found the original code here
I understand that I need to fill before calculating the CRC. The init function is this:
P_CCITT = 0x1021 and crc_tabccitt an array with 256 elements. The idea is to fill that array. What I really don't get is: do those elements need to have 4 bytes? My PHP implementation throws longer values. Would you explain this function to me? I've read a lot and I don't seem to get the whole story which is way I can't do the same in PHP.
Code:
static void init_crcccitt_tab( void ) {
int i, j;
unsigned short crc, c;
for (i=0; i<256; i++) {
crc = 0;
c = ((unsigned short) i) << 8;
for (j=0; j<8; j++) {
if ( (crc ^ c) & 0x8000 ) crc = ( crc << 1 ) ^ P_CCITT;
else crc = crc << 1;
c = c << 1;
}
crc_tabccitt[i] = crc;
}
crc_tabccitt_init = TRUE;
}
Thanks a lot in advance :)
Reine.