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).
- The message passed in is a
UInt8
byte array:[UInt8]
- 3 variables are created as
UInt32
values;crcVal
,byteVal
andmask
crcVal
is then set to the max value ofUInt32
type- each byte from the
UInt8
byte array passed in is then looped through and some kind of operation is performed to produce and append to the final outcome of thecrcVal
value which is ultimately used to send via CoreBluetooth'swriteData
command to aCBPeripheral
.
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;
}