I'm trying to implement a CRC-CCITT (XModem) check in php without success. Does anyone know how to do it? I expected crc16('test') will return 0x9B06
.
Asked
Active
Viewed 734 times
0

Al Foиce ѫ
- 4,195
- 12
- 39
- 49

Dmitriy Antonenko
- 201
- 1
- 8
-
This one looks straight forward http://stackoverflow.com/questions/1834541/crc-4-implementation-in-c-sharp it's C# but looks like it should be simple to adopt for PHP – mabe.berlin Oct 03 '16 at 20:01
-
It's not implementation XModex algorithm. – Dmitriy Antonenko Oct 03 '16 at 20:34
1 Answers
1
Here is a simple bit-wise calculation of the XMODEM 16-bit CRC, in C:
#include <stdint.h>
unsigned crc16xmodem_bit(unsigned crc, void const *data, size_t len) {
if (data == NULL)
return 0;
while (len--) {
crc ^= (unsigned)(*(unsigned char const *)data++) << 8;
for (unsigned k = 0; k < 8; k++)
crc = crc & 0x8000 ? (crc << 1) ^ 0x1021 : crc << 1;
}
crc &= 0xffff;
return crc;
}
This was generated by my crcany software, which also generates byte-wise and word-wise versions for speed.
This can be easily converted to php.

Mark Adler
- 101,978
- 13
- 118
- 158