crc16, crc16file

Calculates the CRC-16-CCITT of a string or a file. (version 4.78 or later)

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:

10001000000100001 (x16+x12+x5+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 CRC16POLY1  0x1021U
#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