function TypedArrays(Type,Bin,begin,Num) { switch(Type) { case 0: return new Int8Array(Bin, begin ,Num)[0]; case 1: return new Uint8Array(Bin, begin ,Num)[0]; case 2: return new Int16Array(Bin, begin ,Num)[0]; case 3: return new Uint16Array(Bin, begin ,Num)[0]; case 4: return new Int32Array(Bin, begin ,Num)[0]; case 5: return new Uint32Array(Bin, begin ,Num)[0]; case 6: return new Float32Array(Bin, begin ,Num)[0]; case 7: return new Float64Array(Bin, begin ,Num)[0]; default: return -1; } }
var crc16= function (buffer,len) { var crc = 0xFFFF; for (var pos = 0; pos < len; pos++) { var x= 0x0000; x=TypedArrays(1,buffer,pos,1); crc ^= x// XOR byte into least sig. byte of crc for (var i = 8; i != 0; i--) // Loop over each bit { if ((crc & 0x0001) != 0) // If the LSB is set { crc >>= 1; // Shift right and XOR 0xA001 crc ^= 0xA001; } else // Else LSB is not set { crc >>= 1; // Just shift right } } } //高低字节转换 return crc; }