Confusing 'string' on 'string' errors

This says ERROR: bitwise op between ‘string’ and ‘string’ on line 88 (which is the first for() loop). data is “binary: 00 00 81 ff ff ff” and len is 2.
`function crc8(data, len) {

  • CRC-8 formula from page 14 of SHT spec pdf
  • Test data 0xBE, 0xEF should yield 0x92
  • Initialization data 0xFF
  • Polynomial 0x31 (x8 + x5 +x4 +1)
  • Final XOR 0x00

local POLYNOMIAL = (0x131);
local crc = (0xFF);

for(local byteCtr = 0; byteCtr < len; byteCtr++)
crc = crc ^ (data[byteCtr].tointeger());
for(local bit = 8; bit > 0; --bit)
if(crc & 0x80) crc = (crc << 1) ^ POLYNOMIAL;
else crc = (crc << 1);
return crc;`

Check what you are passing to crc8. You might find that you’re passing a string rather than an integer as the second argument, ‘len’ .

As another tip, be very careful about using ‘len’ as the name of a variable. Since it’s also the name of a method for all of the non scalar types in Squirrel, you can get some strange errors. I always use the term ‘length’ instead.

Changing len to length resolved this.