Search code examples
vb.netcrc16

crc16 XMODEM from hexstring [Vb.net


I want to figure out how to CRC16 XMODEM works and write a code for it. it will calculate from 3 to 18bytes and calls with the button, it will take HEX values then show a result in hex value aswell. For example: 0x05 0x02 0xAA 0xAA - will be 0x3430 accrording to http://crccalc.com/ - and this is correct. But how to implement this with code , does anyone have any info please?


Solution

  • unsigned crc16xmodem(unsigned crc, unsigned char const *data, size_t len) {
        if (data == NULL)
            return 0;
        while (len--) {
            crc ^= (unsigned)(*data++) << 8;
            for (unsigned k = 0; k < 8; k++)
                crc = crc & 0x8000 ? (crc << 1) ^ 0x1021 : crc << 1;
        }
        return crc & 0xffff;
    }