Thread: CRC Calculator (convert C to PHP)

  1. #1
    Registered User
    Join Date
    Apr 2010

    CRC Calculator (convert C to PHP)


    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.

    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

  2. #2
    Registered User
    Join Date
    Sep 2008
    Toronto, Canada
    I can't vouch for what data type crc_tabccitt[] is because it's not defined in the sample you gave. However, the variable crc that's assigned to each of its elements is unsigned short. That's a two-byte value. Which is correct for a 16-bit CRC as I remember them.

    Not sure what else you're asking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CRC stuff
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 02-07-2010, 11:19 AM
  2. CRC Check
    By silentkarma in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2008, 04:20 AM
  3. PHP installation
    By ssharish2005 in forum Tech Board
    Replies: 8
    Last Post: 11-23-2007, 09:42 PM
  4. PHP on my Computer!
    By xxxrugby in forum Tech Board
    Replies: 4
    Last Post: 03-15-2005, 09:34 AM
  5. PHP 4.3.0 released
    By codingmaster in forum A Brief History of
    Replies: 5
    Last Post: 12-30-2002, 07:40 AM