// // NSData+CRC16.m // LipuWaterPurifier // // Created by leon on 2017/3/6. // Copyright © 2017年 Lipu. All rights reserved. // #import "NSData+CRC16.h" #define CRC_16 0x8005 @implementation NSData (CRC16) #define PLOY 0X1021 uint16_t gen_crc16_ccitt(const uint8_t *data, uint16_t size) { uint16_t crc = 0; uint8_t i; for (; size > 0; size--) { crc = crc ^ (*data++ <<8); for (i = 0; i < 8; i++) { if (crc & 0X8000) { crc = (crc << 1) ^ PLOY; }else { crc <<= 1; } } crc &= 0XFFFF; } return crc; } uint16_t xmodem_crc16_ccitt(const uint8_t *pbBuf, uint32_t dwLen) { uint16_t wCRC = 0; uint16_t bCycle; while (dwLen-- > 0) { wCRC ^= (unsigned short) *pbBuf++ << 8; for (bCycle = 0; bCycle < 8; bCycle++) { if (wCRC & 0x8000) { wCRC = wCRC << 1 ^ 0x1021; } else { wCRC <<= 1; } } } return wCRC; } - (unsigned short)crc16Checksum2 { const uint8_t *bytes = (const uint8_t *)[self bytes]; uint16_t length = (uint16_t)[self length]; return (unsigned short)gen_crc16_ccitt(bytes, length); } - (unsigned short)crc16Checksum { const uint8_t *bytes = (const uint8_t *)[self bytes]; uint16_t length = (uint16_t)[self length]; return (unsigned short)gen_crc16(bytes, length); } uint16_t gen_crc16(const uint8_t *data, uint16_t size) { uint16_t out = 0; int bits_read = 0, bit_flag; /* Sanity check: */ if(data == NULL) return 0; while(size > 0) { bit_flag = out >> 15; /* Get next bit: */ out <<= 1; out |= (*data >> bits_read) & 1; // item a) work from the least significant bits /* Increment bit counter: */ bits_read++; if(bits_read > 7) { bits_read = 0; data++; size--; } /* Cycle check: */ if(bit_flag) out ^= CRC_16; } // item b) "push out" the last 16 bits int i; for (i = 0; i < 16; ++i) { bit_flag = out >> 15; out <<= 1; if(bit_flag) out ^= CRC_16; } // item c) reverse the bits uint16_t crc = 0; i = 0x8000; int j = 0x0001; for (; i != 0; i >>=1, j <<= 1) { if (i & out) crc |= j; } return crc; } //十六进制转ASCII - (NSString *)yx_bluetoothString { Byte *byte = (Byte *)self.bytes; NSMutableString *s = [NSMutableString new]; for (NSInteger i = 0; i < self.length; i++) { [s appendFormat:@"%c",byte[i]]; } return s; } @end