# crc16, crc16file

Calculates the CRC-16-IBM of a string or a file.

    crc16 <intvar> <string>
    crc16file <intvar> <filename>

## Remarks

This macro function calculates the CRC(Cyclic Redundancy Checking) of a string or a file. The polynomial expression(right rotation) is as follows:
0xA001 (x16+x15+x2+x0)
The calculated value stores the variable "intvar" as mathematical value.
If the <filename> file can not open by using crc16file, the system variable "result" is set to -1.

The CRC algorithm implementation by C language is as follows:

    static unsigned int crc16(int n, unsigned char c[])
    {
    #define CRC16POLY2  0x8408U  /* left-right reversal */

    	int i, j;
    	unsigned long r;

    	r = 0xFFFFU;
    	for (i = 0; i < n; i++) {
    		r ^= c[i];
    		for (j = 0; j < CHAR_BIT; j++)
    			if (r & 1) r = (r >> 1) ^ CRC16POLY2;
    			else       r >>= 1;
    	}
    	return r ^ 0xFFFFU;
    }

## Example

    str = 'this is a test string to be crc16ed'
    crc16 crc str

    ; Display crc16 result asHEX
    sprintf '0x%08X' crc
    messagebox inputstr 'crc16 = '

    crc16file crc 'foo.bin'
    if result = -1 then
        messagebox 'file open error' 'crc16 = '
    else
        sprintf '0x%08X' crc
        messagebox inputstr 'crc16 = '
    endif