Swift 4 中的 CRC-16/CCITT

有人

如何让这段代码在 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)

我收到 2 个错误:

"Binary operator'^=' cannot be applied to operands of type 'UInt16' and 'UInt32'

"Binary operator'&' cannot be applied to operands of type 'UInt16' and 'UInt32'
阿米尔

您可以使用UInt16(byte)代替UInt32(byte)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章