天下

记录修行的印记

CRC16算法

#include "stdafx.h"

unsigned 
int GetCrc32(char* pdata,unsigned int len){   
    
//生成Crc32的查询表
    unsigned int Crc32Table[256]; 
    
int i,j;   
    unsigned 
int Crc;   
    
for (i = 0; i < 256; i++){   
        Crc 
= i;   
        
for (j = 0; j < 8; j++){   
            
if (Crc & 1)   
                Crc 
= (Crc >> 1^ 0xEDB88320;   
            
else  
                Crc 
>>= 1
        }   
        Crc32Table[i] 
= Crc;   
    }   
    
//开始计算CRC32校验值
    Crc=0xffffffff;   
    
for(int i=0; i<len; i++)
    {     
        Crc 
= (Crc >> 8^ Crc32Table[(Crc & 0xFF^ pdata[i]];   
    }
    
    Crc 
^= 0xFFFFFFFF;
    
return Crc;   
}   

static const unsigned short Crc16Table[256= 
{                      
    
0x00000xC0C10xC1810x01400xC3010x03C00x02800xC241
    
0xC6010x06C00x07800xC7410x05000xC5C10xC4810x0440
    
0xCC010x0CC00x0D800xCD410x0F000xCFC10xCE810x0E40
    
0x0A000xCAC10xCB810x0B400xC9010x09C00x08800xC841
    
0xD8010x18C00x19800xD9410x1B000xDBC10xDA810x1A40
    
0x1E000xDEC10xDF810x1F400xDD010x1DC00x1C800xDC41
    
0x14000xD4C10xD5810x15400xD7010x17C00x16800xD641
    
0xD2010x12C00x13800xD3410x11000xD1C10xD0810x1040
    
0xF0010x30C00x31800xF1410x33000xF3C10xF2810x3240
    
0x36000xF6C10xF7810x37400xF5010x35C00x34800xF441
    
0x3C000xFCC10xFD810x3D400xFF010x3FC00x3E800xFE41
    
0xFA010x3AC00x3B800xFB410x39000xF9C10xF8810x3840
    
0x28000xE8C10xE9810x29400xEB010x2BC00x2A800xEA41
    
0xEE010x2EC00x2F800xEF410x2D000xEDC10xEC810x2C40
    
0xE4010x24C00x25800xE5410x27000xE7C10xE6810x2640
    
0x22000xE2C10xE3810x23400xE1010x21C00x20800xE041
    
0xA0010x60C00x61800xA1410x63000xA3C10xA2810x6240
    
0x66000xA6C10xA7810x67400xA5010x65C00x64800xA441
    
0x6C000xACC10xAD810x6D400xAF010x6FC00x6E800xAE41
    
0xAA010x6AC00x6B800xAB410x69000xA9C10xA8810x6840
    
0x78000xB8C10xB9810x79400xBB010x7BC00x7A800xBA41
    
0xBE010x7EC00x7F800xBF410x7D000xBDC10xBC810x7C40
    
0xB4010x74C00x75800xB5410x77000xB7C10xB6810x7640
    
0x72000xB2C10xB3810x73400xB1010x71C00x70800xB041
    
0x50000x90C10x91810x51400x93010x53C00x52800x9241
    
0x96010x56C00x57800x97410x55000x95C10x94810x5440
    
0x9C010x5CC00x5D800x9D410x5F000x9FC10x9E810x5E40
    
0x5A000x9AC10x9B810x5B400x99010x59C00x58800x9841
    
0x88010x48C00x49800x89410x4B000x8BC10x8A810x4A40
    
0x4E000x8EC10x8F810x4F400x8D010x4DC00x4C800x8C41,  
    
0x44000x84C10x85810x45400x87010x47C00x46800x8641
    
0x82010x42C00x43800x83410x41000x81C10x80810x4040 
};

//生成Crc16的查询表
void GetCrc16Table()
{   
    unsigned 
short crc16tbl[256]; 
    unsigned 
int i,j;
    unsigned 
short Crc;
    printf(
"unsigned short Crc16Table[256] = \r\n{\r\n");
    
for (i = 0; i < 256; i++) {  
        Crc 
= i;   
        
for (j = 0; j < 8; j++) {   
            
if(Crc & 0x1)   
                Crc 
= (Crc >> 1^ 0xA001;   
            
else  
                Crc 
>>= 1
            
        }
        printf(
"0x%04X,",Crc);
        
if ((i+1)%8==0)
        {
            printf(
"\r\n");
        }
        crc16tbl[i] 
= Crc;
    }
    printf(
"};\r\n");
}
unsigned 
short GetCrc16(char* pdata,unsigned int len)
{   
    
//开始计算CRC16校验值
    unsigned short crc16=0x0000;       
    
for(int i=0; i<len; i++){     
        crc16 
= (crc16 >> 8^ Crc16Table[(crc16 & 0xFF^ pdata[i]];  
    }
    
//Crc ^= 0x0000;  
    return crc16;   
}  

unsigned 
short GetCrc16(unsigned short poly,char* pdata,unsigned int len)
{   
    unsigned 
short crc16;   
    
//开始计算CRC16校验值
    crc16=poly;       
    
for(int i=0; i<len; i++){     
        crc16 
= (crc16 >> 8^ Crc16Table[(crc16 & 0xFF^ pdata[i]];  
    }
    
//Crc ^= 0x0000;  
    return crc16;   
}

 

//test code
#include "stdafx.h"

int main( int argc,char* argv[] )
{
    
char str[]="iawene";
    unsigned 
short crc1 = 0;
    unsigned 
short crc2 = 0;
    GetCrc16Table();
    puts(
"\r\n\r\n\r\n-------------------------------------------");


    crc1
=GetCrc16(str,5);//0x5359
    printf("<1>0x%04X\n",crc1);
    
    
for(int i=0;i<5;i++)
    {
        crc2 
= GetCrc16(crc2,&str[i],1);
    }
    printf(
"<2>0x%04X\n",crc2);

    
    
return 0;
}

posted on 2012-08-17 09:36 天下 阅读(5383) 评论(0)  编辑 收藏 引用 所属分类: 算法


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2015年12月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

常用链接

留言簿(4)

随笔分类(378)

随笔档案(329)

链接

最新随笔

搜索

最新评论