Search code examples
phpccrccode-conversion

Converting 8 bit CRC function written in C to PHP


I am trying to convert a C function to PHP that does 8 bit CRC calculation.

The original C code:

uint8_t CRCCalc (uint8_t* pointer, uint16_t len) {

    uint8_t CRC = 0x00;

    uint16_t tmp;

    while(len > 0) {

        tmp = CRC << 1;

        tmp += *pointer;

        CRC = (tmp & 0xFF) + (tmp >> 8);

        pointer++;

        --len;

    }

    return CRC;

}

The PHP code that I have come up with is:

function crc8_calc($hex_string)
{
    $bin_data = pack('H*',$hex_string);
    $bin_length = strlen($bin_data);

    $CRC    =   0x00;
    $pos    =   0;

    while($bin_length>0)
    {
        //$pos = $CRC << 1;

        $CRC = ($bin_data[$pos] & 0xFF) + ($bin_data[$pos] >> 8);
        $bin_length --;
        $pos++ ;
    }

    return $CRC;
}

There is something that is missing as the results from the PHP functions are not correct. I am not very familiar with C, so not sure if my conversion is correct. The C function gives out the correct CRC

For example, if the hex representation of the string is: 280500000805151001240000000010017475260004041001372068828503000000000000

The CRC should be D4.

I have already seen the following links for CRC8 calculation, but I seem to missing something

how to generate 8bit crc in php CRC8-Check in PHP

I have taken some bits of my conversion code from this answer too Convert C to PHP for CRC16 Function


Solution

  • Try it like this:

    function crc8_calc($hex_string)
    {
        $bin_data = pack('H*',$hex_string);
        $bin_length = strlen($bin_data);
    
        $CRC = 0;
        $tmp = 0;
        $pos = 0;
    
        while($bin_length>0)
        {
            $tmp = $CRC << 1;
            $tmp = $tmp + ord($bin_data[$pos]); //Added ord
    
            $CRC = ($tmp + ($tmp >> 8)) & 0xFF;
            $bin_length --;
            $pos++ ;
        }
        return $CRC;
    }