# CRC 16 -DECT with poly x^16 + x^10 + x^8 + x^7 + x^3 + 1

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.

## 评论

### Some CRCs are defined to pr

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)
\$
``````

### Try changing 0x10589 to 0xA0

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;
}
``````

### This code works everytime, b

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);
}
``````