在C#中实现CRC32算法。

    技术2022-05-11  117

     

    using  System; using  System.Collections.Generic; using  System.Text; using  System.IO; namespace  GetCRC32... {     class  CRC32Cls    ... {         protected   ulong [] Crc32Table;         // 生成CRC32码表          public   void  GetCRC32Table()         ... {             ulong  Crc;            Crc32Table  =   new   ulong [ 256 ];             int  i,j;             for (i  =   0 ;i  <   256 ; i ++ )             ... {                Crc  =  ( ulong )i;                 for  (j  =   8 ; j  >   0 ; j -- )                ... {                     if  ((Crc  &   1 ==   1 )                        Crc  =  (Crc  >>   1 ^   0xEDB88320 ;                     else                         Crc  >>=   1 ;                }                 Crc32Table[i]  =  Crc;            }         }          // 获取字符串的CRC32校验值          public   ulong  GetCRC32Str( string  sInputString)        ... {             // 生成码表             GetCRC32Table();             byte [] buffer  =  System.Text.ASCIIEncoding.ASCII.GetBytes(sInputString);             ulong  value  =   0xffffffff ;             int  len  =  buffer.Length;             for  ( int  i  =   0 ; i  <  len; i ++ )            ... {                value  =  (value  >>   8 ^  Crc32Table[(value  &   0xFF ) ^  buffer[i]];            }              return  value  ^   0xffffffff ;         }     } }

    调用方法:

             private   void  button1_Click( object  sender, EventArgs e)        ... {            CRC32Cls CRC  =   new  CRC32Cls();            textBox2.Text  =  String.Format( " {0:X8} " , CRC.GetCRC32Str(textBox1.Text));        }

    最新回复(0)