2

believe me I have tried to code this, tried Google, and haven't had any luck. I'm trying to implement a CRC16 using this poly

x^16 + x^10 + x^8 + x^7 + x^3 + 1

using the C language. Since I understand PHP better I'm trying to get a function going, but I'm not getting the right answer of 28713. This code is generating a CRC of 32713.

function crc16($string,$crc=0) {

for ( $x=0; $x<strlen( $string ); $x++ ) {

    $crc = $crc ^ ord( $string[$x] );
    echo $crc.'<br />';
    for ($y = 0; $y < 8 ; $y++) {

        if ( ($crc & 0x0001) == 0x0001 ) $crc = ( ($crc >> 1 ) ^ 0x10589  );
        else                             $crc =    $crc >> 1;
    }
}

    return $crc;
}


echo 'CRC:'.crc16('10100011');

Please I beg anyone to give a hand with this..thanks in advance.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Nick
  • 59
  • 2
  • 9

3 Answers3

4
  1. Some CRCs are defined to process the bits from each byte from MSB to LSB, and some are defined to process bits from LSB to MSB (the latter is generally the order which is described as "reflected" and uses a reversed polynomial). Your code puts new bits in at the LSB end of the CRC and shifts right, which is suitable for a reflected CRC, but CRC-16-DECT appears to be one of the non-reflected ones.

  2. Your input of "10100011" suggests binary, but is being processed as an 8-byte ASCII string.

To see what happens when treating 10100011 as binary instead, and working from MSB first, here's a hand calculation (as 8 bits of input doesn't require very much effort):

polynomial coefficients
        |
        |            10100010  <--- quotient (irrelevant)
        v          __________
 10000010110001001 ) 10100011  <-------- input
                   ^ 10000010110001001
                     -----------------
                   =   100001110001001
                     ^ 10000010110001001
                       -----------------
                     =      101110101101
                          ^ 10000010110001001
                            -----------------
   remainder (CRC) -----> =   111000000101001
     = 0x7029 = 28713

So treating the input as binary and working MSB first is the right thing to do.

Here is some C code to do the job (as I'm not really into PHP, and ultimately you want C code anyway):

#include <stdio.h>
#include <stdint.h>

static uint16_t crc16(const uint8_t *data, size_t len)
{
    size_t i, j;
    uint16_t crc = 0;

    for (i = 0; i < len; i++) {
        crc ^= (data[i] << 8);              /* data at top end, not bottom */
        for (j = 0; j < 8; j++) {
            if ((crc & 0x8000) == 0x8000)   /* top bit, not bottom */
                crc = (crc << 1) ^ 0x0589;  /* shift left, not right */
            else
                crc <<= 1;                  /* shift left, not right */
        }
    }

    return crc;
}

int main(void)
{
    const uint8_t in[] = { 0xa3 };          /* = 10100011 in binary */
    uint16_t crc = crc16(in, sizeof(in));

    printf("%u (0x%x)\n", crc, crc);
    return 0;
}

Result:

$ gcc -Wall -o crc16 crc16.c
$ ./crc16  
28713 (0x7029)
$ 
Matthew Slattery
  • 45,290
  • 8
  • 103
  • 119
  • Matthew, dude. I owe you a drink. Works perfect thanks a lot bud, and this cleared a lot of questions I had about CRC. – Nick Nov 27 '10 at 20:13
  • Let say i do not know the size of the data, instead of 8 bits, i get 16 bits...0xA4321. – Nick Nov 28 '10 at 02:14
  • Not sure I understand what you mean by that. `crc16()` takes a pointer to a buffer of bytes, and a length in bytes - so if you need a CRC over 16 bits (0xA4321 is 20 bits!), pass in a 2 byte buffer and a length of 2. (In my example, I've used `sizeof(in)` to work out the length of the array automatically, so you can just change `{ 0xa3 }` to `{ 0xa3, 0x21 }` or whatever.) – Matthew Slattery Nov 28 '10 at 14:55
  • the input file will have data like this,0123456789ABCEDF. I need to be able to break that down and pass it... – Nick Nov 28 '10 at 17:11
  • thanks for taking the time to give me a hand with this. your code works just like you said it would. my problem is that c is not as dynamic as php, for instance I need to turn a string like this "0123456789ABCDEF" to in[] = { 0x01,0x23,0x45,0x67,0x89,0xAB,0xCE,0xDF }; – Nick Nov 28 '10 at 17:26
  • OK. Parsing a hex string into a byte array is a completely separate problem... searching around SO, it's a question that's been asked before: see e.g. [here](http://stackoverflow.com/q/3408706). (If you have further problems, or issues with other parts of your code, it's probably worth asking a new question.) Please consider [accepting](http://meta.stackexchange.com/q/5234) this answer, now that the CRC stuff works! – Matthew Slattery Nov 28 '10 at 20:35
0

Try changing 0x10589 to 0xA001:

function crc16($string,$crc=0) {

    for ( $x=0; $x<strlen( $string ); $x++ ) {

        $crc = $crc ^ ord( $string[$x] );
        for ($y = 0; $y < 8; $y++) {

            if ( ($crc & 0x0001) == 0x0001 ) $crc = ( ($crc >> 1 ) ^ 0xA001 );
            else                             $crc =    $crc >> 1;
        }
    }

    return $crc;
}
wajiw
  • 12,239
  • 17
  • 54
  • 73
0

This code works everytime, but I'm not exactly understanding what's going on.

  char *MakeCRC(char *BitString)
   {
  static char Res[17];                                 // CRC Result
  char CRC[16];
    int  i;
  char DoInvert;

   for (i=0; i<16; ++i)  CRC[i] = 0;                    // Init before calculation

   for (i=0; i<strlen(BitString); ++i)
    {
   DoInvert = ('1'==BitString[i]) ^ CRC[15];         // XOR required?

  CRC[15] = CRC[14];
  CRC[14] = CRC[13];
  CRC[13] = CRC[12];
  CRC[12] = CRC[11];
  CRC[11] = CRC[10];
  CRC[10] = CRC[9] ^ DoInvert;
  CRC[9] = CRC[8]; 
  CRC[8] = CRC[7] ^ DoInvert;
  CRC[7] = CRC[6] ^ DoInvert;
  CRC[6] = CRC[5];
  CRC[5] = CRC[4];
  CRC[4] = CRC[3];
  CRC[3] = CRC[2] ^ DoInvert;
  CRC[2] = CRC[1];
  CRC[1] = CRC[0];
  CRC[0] = DoInvert;
  }

  for (i=0; i<16; ++i)  Res[15-i] = CRC[i] ? '1' : '0'; // Convert binary to ASCII
  Res[16] = 0;                                         // Set string terminator

    return(Res);
    }


     // A simple test driver:

    #include <stdio.h>

   int main()
    {
    char *Data, *Result;                                       // Declare two strings

    Data = "1101000101000111";
    Result = MakeCRC(Data);                                    // Calculate CRC

    printf("CRC of [%s] is [%s] with P=[10000010110001001]\n", Data, Result);

     return(0);
      }
Nick
  • 59
  • 2
  • 9