How to get this code working in swift 4:
func crc16ccitt(data: [UInt8], seed: UInt16 = 0x1d0f, final: UInt16 = 0xffff) -> UInt16 {
var crc = seed
data.forEach { (byte) in
crc ^= UInt32(byte) << 8
(0..<8).forEach({ _ in
crc = (crc & UInt32(0x8000)) != 0 ? (crc << 1) ^ 0x1021 : crc << 1
})
}
return UInt16(crc & final)
}
print(crc16ccitt(data: "Karim".utf8.map{$0}) == 0x792C)
I get 2 errors:
"Binary operator'^=' cannot be applied to operands of type 'UInt16' and 'UInt32'
"Binary operator'&' cannot be applied to operands of type 'UInt16' and 'UInt32'
You can use UInt16(byte)
instead of UInt32(byte)