I want to calculate checksum. My expected answer of crc is 0xE1 But when I run this code it returns 0. I am not getting any solution to do this. Please help me to solve this. Below is my tried code.
My viewController:
class ViewController: UIViewController {
var checkSum:UInt8 = 0
override func viewDidLoad() {
super.viewDidLoad()
let bytes = [0x01,0x0B,0x00,0x0B,0x03,0x07,0x12,0x0E,0x0C,0x10,0x09,0x12,0x0C,0x19,0x09,0xFF,0x14]
for item in bytes {
print(calculateCheckSum(crc: checkSum, byteValue: UInt8(item)))
}
}
func calculateCheckSum(crc:UInt8, byteValue: UInt8) -> UInt8 {
let generator = 0x1D
checkSum = crc ^ byteValue
for _ in 1...8 {
if (crc & 0x80 != 0) {
checkSum = (crc << 1) ^ UInt8(Int8(generator))
}
else {
checkSum <<= UInt8(1)
}
}
return crc
}
}