Zoltan Hudak / CRC16_CCITT

Files at this revision

API Documentation at this revision

Comitter:
hudakz
Date:
Sun Sep 03 08:29:38 2017 +0000
Parent:
0:253105d48c3c
Commit message:
Function parameter added for setting initial CRC value. Defaults to 0x0000.

Changed in this revision

CRC16_CCITT.cpp Show annotated file Show diff for this revision Revisions of this file
CRC16_CCITT.h Show annotated file Show diff for this revision Revisions of this file
diff -r 253105d48c3c -r 6ecc3a64bf7b CRC16_CCITT.cpp
--- a/CRC16_CCITT.cpp	Sat Sep 02 08:26:22 2017 +0000
+++ b/CRC16_CCITT.cpp	Sun Sep 03 08:29:38 2017 +0000
@@ -4,7 +4,7 @@
  *
  * Modified by Zoltan Hudak to implement CRC16-CCITT
  * using the polynomial 0x1021: X^16 + X^15 + X^2 + 1.
- * Initial CRC register = 0x0000
+ * Default initial CRC value = 0x0000
  */
 
 #include "CRC16_CCITT.h"
@@ -46,14 +46,15 @@
     0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
 };
 
-unsigned short CRC16_CCITT::calc(char input[], int length) {
-    unsigned short  result = 0;
+unsigned short CRC16_CCITT::calc(char input[], int length, unsigned short crc /*=0x0000*/) {
     for(int i = 0; i < length; i++) {
-        unsigned short  tableValue = TABLE[((result >> 8) ^ *(char*)input++) & SHIFTER];
-        result = (result << 8) ^ tableValue;
+        unsigned short  tableValue = TABLE[((crc >> 8) ^ *(char*)input++) & SHIFTER];
+        crc = (crc << 8) ^ tableValue;
     }
 
-    return result;
+    return crc;
 }
 
 
+
+
diff -r 253105d48c3c -r 6ecc3a64bf7b CRC16_CCITT.h
--- a/CRC16_CCITT.h	Sat Sep 02 08:26:22 2017 +0000
+++ b/CRC16_CCITT.h	Sun Sep 03 08:29:38 2017 +0000
@@ -4,7 +4,7 @@
  *
  * Modified by Zoltan Hudak to implement CRC16-CCITT
  * using the polynomial 0x1021: X^16 + X^15 + X^2 + 1.
- * Initial CRC register = 0x0000
+ * Default initial CRC value = 0x0000
  */
 
 #ifndef CRC16_CCITT_H
@@ -16,8 +16,9 @@
     static const unsigned int   SHIFTER;
     static const unsigned short TABLE[];
 public:
-    CRC16_CCITT(void) { };
-    ~CRC16_CCITT(void){ };
-    unsigned short  calc(char input[], int length);
+    CRC16_CCITT(void){};
+    ~CRC16_CCITT(void){};
+    unsigned short calc(char input[], int length, unsigned short crc = 0x0000);
 };
+
 #endif // CRC16_CCITT_H