如何在swift中使用zlib中的crc32(Xcode 9)

时间:2018-03-02 15:57:18

标签: swift zlib xcode9 crc32

我是swift / xcode的新手,我正在尝试使用crc32函数,正如所提到的here,它是在libz.dylib的zlib中提供的。

我修改了代码,以便我现在正在尝试:

let message1 = "some message".first?.value!.data(using: String.Encoding.utf8, allowLossyConversion: false) let crc = crc32(CLong(0), UnsafePointer<Bytef>(strcat!.bytes), UInt(message1!.length))

但是我遇到了错误:use of unresolved identifier 'crc32'

有没有人可以帮我弄清楚在尝试使用crc32函数时我可能会缺少哪一步?

谢谢!

1 个答案:

答案 0 :(得分:0)

在 Swift 5 中:

import zlib

let data = Data(base64Encoded: "SGF2ZSBhIG5pY2UgZGF5ISA6KQ==")!
let checksum = data.withUnsafeBytes { crc32(0, $0.bindMemory(to: Bytef.self).baseAddress, uInt(data.count)) }
print("crc32: 0x\(String(format:"%08X", checksum))")