Implementation of the CRC32/CRC16 algorithm in C #

Source: Internet
Author: User
Tags crc32 crc32 checksum

CRC32 algorithm

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Text;4 usingSystem.IO;5 6 namespaceGetCRC327 {8     classCrc32cls9     {Ten         protected ULONG[] crc32table; One         //Generate CRC32 Code table A          Public voidgetcrc32table () -         { -             ULONGCRC; theCrc32table =New ULONG[ the]; -             inti,j; -              for(i =0; I < the; i++)  -             { +CRC = (ULONG) I; -                  for(j =8; J >0; j--) +                 { A                     if(CRC &1) ==1) atCRC = (CRC >>1) ^0xedb88320; -                     Else -CRC >>=1; -                 } -Crc32table[i] =CRC; -             } in         } -  to         //gets the CRC32 checksum value of the string +          Public ULONGGETCRC32STR (stringsinputstring) -         { the             //Generate Code Table * getcrc32table (); $             byte[] buffer =System.Text.ASCIIEncoding.ASCII.GetBytes (sinputstring);Panax Notoginseng             ULONGValue =0xFFFFFFFF; -             intLen =buffer. Length; the              for(inti =0; i < Len; i++) +             { AValue = (Value >>8) ^ crc32table[(value &0xFF)^Buffer[i]]; the             } +             returnValue ^0xFFFFFFFF;  -         } $     } $}

CRC16 algorithm

1           Public Static byte[] CRC16 (stringsinputstring)2          {3              byte[] data =System.Text.ASCIIEncoding.ASCII.GetBytes (sinputstring);4              intLen =data. Length;5              if(Len >0)6              {7                  ushortCRC =0xFFFF;8 9                   for(inti =0; i < Len; i++)Ten                  { OneCRC = (ushort) (CRC ^(Data[i])); A                       for(intj =0; J <8; J + +) -                      { -CRC = (CRC &1) !=0? (ushort) (CRC >>1) ^0xa001) : (ushort) (CRC >>1); the                      } -                  } -                  byteHi = (byte) (CRC &0xff00) >>8);//High Position -                  byteLo = (byte) (CRC &0x00FF);//Low Position +  -                  return New byte[] {hi, lo}; +              } A              return New byte[] {0,0 }; at          } -  - //ASCII code to string -           Public Static stringBytetostring (byte[] arr,BOOLisreverse) -          { -              Try in              { -                  bytehi = arr[0], lo = arr[1]; to                  returnConvert.ToString (isreverse Hi + lo *0x100: Hi *0x100+ Lo, -). ToUpper (). PadLeft (4,'0'); +              } -              Catch(Exception ex) {Throw(ex);} the}

Implementation of the CRC32/CRC16 algorithm in C #

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.