avatar

iOS CRC16校验

iOS CRC16校验

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
+(uint16_t)calculateCRC:(NSData *)data start:(int)start end:(int)end{
Byte *bts = (Byte *)[data bytes];
uint16_t crc = (short) 0x0000;
uint8_t i;
for (int ii = start; ii <= end; ii++) {
crc = crc ^ (bts[ii] << 8);
for (i = 0; i < 8; i++) {
if (crc & 0X8000) {
crc = (crc << 1) ^ 0x1021;
} else {
crc <<= 1;
}
}
crc &= 0XFFFF;
}
return crc;
}

NSData *data = [@"abc" dataUsingEncoding:NSUTF8StringEncoding];
uint16_t res = [BluetoothManager calculateCRC:data start:0 end:2];
NSLog(@"calculateCRC %04x", res);
文章作者: pengweifu
文章链接: https://www.pengwf.com/2019/12/11/ios/IOS-CRC/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 麦子的博客
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论