NSData+CRC16.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // NSData+CRC16.m
  3. // LipuWaterPurifier
  4. //
  5. // Created by leon on 2017/3/6.
  6. // Copyright © 2017年 Lipu. All rights reserved.
  7. //
  8. #import "NSData+CRC16.h"
  9. #define CRC_16 0x8005
  10. @implementation NSData (CRC16)
  11. #define PLOY 0X1021
  12. uint16_t gen_crc16_ccitt(const uint8_t *data, uint16_t size) {
  13. uint16_t crc = 0;
  14. uint8_t i;
  15. for (; size > 0; size--) {
  16. crc = crc ^ (*data++ <<8);
  17. for (i = 0; i < 8; i++) {
  18. if (crc & 0X8000) {
  19. crc = (crc << 1) ^ PLOY;
  20. }else {
  21. crc <<= 1;
  22. }
  23. }
  24. crc &= 0XFFFF;
  25. }
  26. return crc;
  27. }
  28. uint16_t xmodem_crc16_ccitt(const uint8_t *pbBuf, uint32_t dwLen)
  29. {
  30. uint16_t wCRC = 0;
  31. uint16_t bCycle;
  32. while (dwLen-- > 0)
  33. {
  34. wCRC ^= (unsigned short) *pbBuf++ << 8;
  35. for (bCycle = 0; bCycle < 8; bCycle++)
  36. {
  37. if (wCRC & 0x8000)
  38. {
  39. wCRC = wCRC << 1 ^ 0x1021;
  40. }
  41. else
  42. {
  43. wCRC <<= 1;
  44. }
  45. }
  46. }
  47. return wCRC;
  48. }
  49. - (unsigned short)crc16Checksum2
  50. {
  51. const uint8_t *bytes = (const uint8_t *)[self bytes];
  52. uint16_t length = (uint16_t)[self length];
  53. return (unsigned short)gen_crc16_ccitt(bytes, length);
  54. }
  55. - (unsigned short)crc16Checksum
  56. {
  57. const uint8_t *bytes = (const uint8_t *)[self bytes];
  58. uint16_t length = (uint16_t)[self length];
  59. return (unsigned short)gen_crc16(bytes, length);
  60. }
  61. uint16_t gen_crc16(const uint8_t *data, uint16_t size)
  62. {
  63. uint16_t out = 0;
  64. int bits_read = 0, bit_flag;
  65. /* Sanity check: */
  66. if(data == NULL)
  67. return 0;
  68. while(size > 0)
  69. {
  70. bit_flag = out >> 15;
  71. /* Get next bit: */
  72. out <<= 1;
  73. out |= (*data >> bits_read) & 1; // item a) work from the least significant bits
  74. /* Increment bit counter: */
  75. bits_read++;
  76. if(bits_read > 7)
  77. {
  78. bits_read = 0;
  79. data++;
  80. size--;
  81. }
  82. /* Cycle check: */
  83. if(bit_flag)
  84. out ^= CRC_16;
  85. }
  86. // item b) "push out" the last 16 bits
  87. int i;
  88. for (i = 0; i < 16; ++i) {
  89. bit_flag = out >> 15;
  90. out <<= 1;
  91. if(bit_flag)
  92. out ^= CRC_16;
  93. }
  94. // item c) reverse the bits
  95. uint16_t crc = 0;
  96. i = 0x8000;
  97. int j = 0x0001;
  98. for (; i != 0; i >>=1, j <<= 1) {
  99. if (i & out) crc |= j;
  100. }
  101. return crc;
  102. }
  103. //十六进制转ASCII
  104. - (NSString *)yx_bluetoothString {
  105. Byte *byte = (Byte *)self.bytes;
  106. NSMutableString *s = [NSMutableString new];
  107. for (NSInteger i = 0; i < self.length; i++) {
  108. [s appendFormat:@"%c",byte[i]];
  109. }
  110. return s;
  111. }
  112. @end