Adler32算法的实现

来源:互联网 发布:财汇金融数据库 编辑:程序博客网 时间:2024/06/07 23:44

转自http://blog.csdn.net/flyfish1986/article/details/2595969

#include <string>
#include <iostream.h>
#include <iomanip.h>
#define BASE 65521 
unsigned long adler32(unsigned char *buf, int len)
{
 
 unsigned long adler=1;
 unsigned long s1 = adler & 0xffff;
    unsigned long s2 = (adler >> 16) & 0xffff;
 
 int i;
 for (i = 0; i < len; i++) 
 {
  s1 = (s1 + buf[i]) % BASE;
  s2 = (s2 + s1) % BASE;
 }
 return (s2 << 16) + s1;
}

int main()
{
 while(1)
 {
  unsigned long Temp=0;
  int len;
  unsigned char *str;
  str = (unsigned char *) malloc(180000);
  cout<<"adler32 checksum:";
  cin>>str;
  len=strlen((const char*)str);
  Temp=adler32(str,len);
  cout<<hex<<Temp<<endl;
  
  
 }
 return 0;
}