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 #