{"id":585,"date":"2013-05-15T16:05:59","date_gmt":"2013-05-15T15:05:59","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=585"},"modified":"2013-05-30T06:05:11","modified_gmt":"2013-05-30T05:05:11","slug":"algorithms-crc16","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=585","title":{"rendered":"Algorithms: CRC16"},"content":{"rendered":"<p>Simple implemetation of 16bit Cyclic Redundancy Check (CRC16) in C++<!--more--><\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full\" alt=\"\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/img_5193a44330f09.png\" \/><\/p>\n<pre lang=\"cpp\">#include &lt;stdio.h&gt;\r\n\r\n#define TRUE 1\r\n#define FALSE 0\r\n\r\n#define TABLE_SIZE 256\r\n#define BUF_SIZE 7\r\n#define POLYNOM 0xA001\r\n\r\nvoid CreateLookUpTable(unsigned int* lookUp) {\r\n  unsigned int CRC = 0;\r\n  unsigned int shiftedCRC = 0;\r\n  for(int byteCount=0; byteCount &lt; TABLE_SIZE; byteCount++) {\r\n    CRC = byteCount;\r\n    for(int bitCount=0; bitCount&lt;8; bitCount++) {\r\n      shiftedCRC = CRC &gt;&gt; 1;\r\n      if((CRC &amp; 0x01)==1)\r\n        CRC = shiftedCRC ^ POLYNOM;\r\n      else\r\n        CRC = shiftedCRC;\r\n    }\r\n    lookUp[byteCount] = CRC;\r\n  }\r\n}\r\n\r\nunsigned int CRCCompute(unsigned int* lookUp, unsigned int init, short* buffer, int length) {\r\n  unsigned int CRC = init;\r\n  unsigned int shiftedCRC = 0;\r\n  for(int pos = 0; pos &lt; length; pos++) {\r\n    shiftedCRC = CRC &gt;&gt; 8;\r\n\r\n    CRC = shiftedCRC ^ lookUp[buffer[pos] ^ (CRC &amp; 0xFF)];\r\n  }\r\n  return CRC;\r\n}\r\n\r\nint main(int argc, char* argv) {\r\n  short buffer[BUF_SIZE];\r\n  buffer[0] = 0xB2;\r\n  buffer[1] = 0x08;\r\n  buffer[2] = 0xFF;\r\n  buffer[3] = 0x01;\r\n  buffer[4] = 0x01;\r\n  buffer[5] = 0x81;\r\n  buffer[6] = 0x00;\r\n\r\n  unsigned int lookUp[TABLE_SIZE];\r\n  CreateLookUpTable(lookUp);\r\n\r\n  unsigned int crc = CRCCompute(lookUp, 0xFFFF, buffer, BUF_SIZE);\r\n  printf(\"CRC Hi: %X\\nCRC Lo: %X\",crc&gt;&gt;8, crc&amp;0xFF);\r\n  getchar();\r\n  return TRUE;\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Simple implemetation of 16bit Cyclic Redundancy Check (CRC16) in C++<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Simple implemetation of 16bit Cyclic Redundancy Check (CRC16) in C++","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15,150,9],"tags":[],"class_list":["post-585","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-c-development","category-development"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-9r","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/585","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=585"}],"version-history":[{"count":2,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/585\/revisions"}],"predecessor-version":[{"id":607,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/585\/revisions\/607"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}