C # crc16 source code

Source: Internet
Author: User

Algorithm:

  Public   Ushort [] Crc16table = {  0x0 , Zero X 1021 , Zero X 2042 , Zero X 3063 , Zero X 4084 , 0x50a5 , 0x60c6 , 0x70e7  ,  Zero X 8108 ,Zero X 9129 , 0xa14a , 0xb16b , 0xc18c , 0xd1ad , 0xe1ce , 0xf1ef  ,  Zero X 1231 , Zero x 210 , Zero X 3273 , Zero X 2252 , 0x52b5 , Zero X 4294 , 0x72f7 , 0x62d6  ,  Zero X 9339 , Zero X 8318 , 0xb37b , 0xa35a , 0xd3bd , 0xc39c , 0xf3ff , 0xe3de  ,  Zero X 2462 , Zero X 3443 ,Zero x 420 , Zero X 1401 , 0x64e6 , 0x74c7 , 0x44a4 , Zero X 5485  ,  0xa56a , 0xb54b , Zero X 8528 , Zero X 9509 , 0xe5ee , 0xf5cf , 0xc5ac , 0xd58d  ,  Zero X 3653 , Zero X 2672 , Zero X 1611 , Zero X 630 , 0x76d7 , 0x66f6 , Zero X 5695 , 0x46b4  ,  0xb75b , 0xa77a , Zero X 9719 ,Zero X 8738 , 0xf7df , 0xe7fe , 0xd79d , 0xc7bc  ,  0x48c4 , 0x58e5 , Zero X 6886 , 0x78a7 , Zero X 840 , Zero X 1861 , Zero X 2802 , Zero X 3823 ,  0xc9cc , 0xd9ed , 0xe98e , 0xf9af , Zero X 8948 , Zero X 9969 , 0xa90a , 0xb92b  ,  0x5af5 , 0x4ad4 , 0x7ab7 , 0x6a96 ,0x1a71 , 0xa50 , 0x3a33 , 0x2a12  ,  0 xdbfd , 0 xcbdc , 0 xfbbf , 0xeb9e , 0x9b79 , 0x8b58 , 0xbb3b , 0xab1a  , 0x6ca6 , 0x7c87 , 0x4ce4 , 0x5cc5 , 0x2c22 , 0x3c03 , 0x600. , 0x1c41  ,  0 xedae , 0xfd8f , 0 xcdec , 0 xddcd , 0xad2a , 0xbd0b , 0x8d68 , 0x9d49  ,  0x7e97 , 0x6eb6 , 0x5ed5 , 0x4ef4 , 0x3e13 , 0x2e32 , 0x1e51 , 0xe70  ,  0xff9f ,0 xefbe , 0 xdfdd , 0 xcffc , 0xbf1b , 0xaf3a , 0x9f59 , 0x8f78  ,  Zero X 9188 , 0x81a9 , 0xb1ca , 0xa1eb , 0xd10c , 0xc12d , 0xf14e , 0xe16f  ,  Zero X 1080 , 0xa1 , 0x30c2 , 0x20e3 , Zero X 5004 , Zero X 4025 , Zero X 7046 , Zero X 6067  ,  0x83b9 , Zero X 9398 ,0xa3fb , 0xb3da , 0xc33d , 0xd31c , 0xe37f , 0xf35e  ,  0x2b1 , Zero X 1290 , 0x22f3 , 0x32d2 , Zero X 4235 , Zero X 5214 , Zero X 6277 , Zero X 7256  ,  0xb5ea , 0xa5cb , 0x95a8 , Zero X 8589 , 0xf56e , 0xe54f , 0xd52c , 0xc50d  ,  0x34e2 , 0x24c3 , 0x14a0 ,Zero X 481 , Zero X 7466 , Zero X 6447 , Zero X 5424 , Zero X 4405  ,  0xa7db , 0xb7fa , Zero X 8799 , 0x97b8 , 0xe75f , 0xf77e , 0xc71d , 0xd73c ,  0x26d3 , 0x36f2 , Zero X 691 , 0x16b0 , Zero X 6657 , Zero X 7676 , Zero X 4615 , Zero X 5634  ,  0xd94c , 0xc96d , 0xf90e , 0xe92f ,0x99c8 , 0x89e9 , 0xb98a , 0xa9ab  ,  Zero X 5844 , Zero X 4865 , Zero X 7806 , Zero X 6827 , 0x18c0 , 0x8e1 , Zero X 3882 , 0x28a3  , 0xcb7d , 0xdb5c , 0xeb3f , 0xfb1e , 0x8bf9 , 0x9bd8 , 0 xabbb , 0xbb9a  ,  0x4a75 , 0x5a54 , 0x6a37 , 0x7a16 , 0xaf1 , 0x1ad0 , 0x2ab3 , 0x3a92  ,  0xfd2e , 0xed0f , 0xdd6c , 0xcd4d , 0 xbdaa , 0xad8b , 0x9de8 , 0x8dc9  ,  0x7c26 ,0x6c07 , 0x5c64 , 0x4c45 , 0x3ca 2 , 0x2c83 , 0x1ce0 , 0xcc0.  ,  0xef1f , 0xff3e , 0xcf5d , 0xdf7c , 0xaf9b , 0 xbfba , 0x8fd9 , 0x9ff8  ,  0x6e17 , 0x7e36 , 0x4e55 , 0x5e74 , 0x2e93 , 0x3eb2 , 0xed1 , 0x1ef0  };  Public   Int Crc16 (byte [] dat, Int  Count ){  Int CRC = 0  ; Byte crctemp;  Int I = 0  ;  While (Count> 0  ) {Count -= 1  ; Crctemp = Convert. tobyte (CRC>8  ); CRC = (CRC < 8 )& 0 xFFFF  ; CRC = (Crc16table [crctemp ^ dat [I] ^ CRC )& 0 xFFFF  ; I + = 1  ;}  Return  CRC ;}  Public   Bool Check (byte [] portdata) {byte [] porttemp = New Byte [ 4  ]; Porttemp [  0 ] = Portdata [ 1  ]; Porttemp [  1 ] = Portdata [ 2  ]; Porttemp [  2 ] = 0  ; Porttemp [  3 ] = 0  ;  Int Crctemp = crc16 (porttemp, 2  ); Porttemp [  2 ] = Convert. tobyte (crctemp> 8  ); Porttemp [  3 ] = Convert. tobyte (crctemp & 255  );  If (Porttemp [ 2 ] = Portdata [3 ] & Porttemp [ 3 ] = Portdata [ 4  ]) {Console. writeline (  "  This is the correct parking card number through CRC verification  "  );  Return   True  ;}  Else  {Console. writeline (  " Cannot pass CRC verification, incorrect parking card  "  );  Return   False  ;}} 

Call:

 
Byte[] Aaff = {0xdd,0xdd,0xff};IntAA =Crc16 (aaff, aaff. Length );StringBB = convert. tostring (AA,16). Toupper ();
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.