I'm trying to convert an Objective C method to Swift but in order to do so I need some help understanding what's going on.
For context, the input to this method begins as an NSString
which is then converted into an NSData
object with utf8 encoding. The bytes of this data object are then passed into the method (the message
parameter).
The return value of this method is then sent via writeData
to a certain characteristic of a CBPeripheral
.
Here's what I understand (from a Swift perspective).
UInt8
byte array: [UInt8]
UInt32
values; crcVal
, byteVal
and mask
crcVal
is then set to the max value of UInt32
typeUInt8
byte array passed in is then looped through and some kind of operation is performed to produce and append to the final outcome of the crcVal
value which is ultimately used to send via CoreBluetooth's writeData
command to a CBPeripheral
.I'm not very solid on bitwise operators or why the below method is doing what it's doing as there's no documentation. Can somebody help clear up any or all parts of this? I'm hoping to write a Swift equivalent version of this method. Thank you.
- (uint32_t) computeCRC:(uint8_t *)message len:(NSUInteger)msgLen {
uint32_t crcVal = 0, byteVal, mask;
crcVal = 0xFFFFFFFF;
for (int i=0; i<msgLen; i++) {
byteVal = message[i];
crcVal ^= byteVal;
for (int j=7; j>=0; j--) {
mask = -(crcVal&1);
crcVal = (crcVal>>1)^(0xEDB88320 & mask);
}
}
return ~crcVal;
}
Instead of writing the implementation from scratch, why don't you use the zlib implementation of crc32
?:
import zlib
func crc32(from data: Data) -> UInt {
return data.withUnsafeBytes { (buffer: UnsafePointer<Bytef>) -> UInt in
return zlib.crc32(0, buffer, uInt(data.count))
}
}
However, to help you understand the bitwise operations:
func computeCRC(message: [UInt8]) -> UInt32 {
var crc: UInt32 = 0xFFFFFFFF
for byte in message {
crc ^= UInt32(byte)
for _ in 0 ..< 8 {
// negation using 2-complement: -x = ~x + 1
// use &+ addition with overflow
let mask = ~(crc & 1) &+ 1
crc = (crc >> 1) ^ (0xEDB88320 & mask)
}
}
return ~crc
}
Note that we don't need to pass the number of bytes in Swift. The implementations have different signatures and return a slightly different type but they both give the same result.