邀朋友种豆,一起分享吧
喜欢购买正品行货?那就去品牌街

一个好用的C语言CRC32校验码程序源码

来自:种豆 时间:2017-12-29 阅读:3507次 原文链接

CRC的全称是循环冗余校验。在数据存储和数据通讯领域,为了保证数据的正确,就不得不采用检错的手段。在诸多检错手段中,CRC32是最著名的一种。以下是一段C语言实现的CRC32算法源码。

#include <stdio.h>
#include
<stdlib.h>

//定义CRC32校验码系数表
unsigned long crctable[256] =
{
0X00000000L, 0X04c11db7L, 0X09823b6eL, 0X0d4326d9L,
0X130476dcL, 0X17c56b6bL, 0X1a864db2L, 0X1e475005L,
0X2608edb8L, 0X22c9f00fL, 0X2f8ad6d6L, 0X2b4bcb61L,
0X350c9b64L, 0X31cd86d3L, 0X3c8ea00aL, 0X384fbdbdL,
0X4c11db70L, 0X48d0c6c7L, 0X4593e01eL, 0X4152fda9L,
0X5f15adacL, 0X5bd4b01bL, 0X569796c2L, 0X52568b75L,
0X6a1936c8L, 0X6ed82b7fL, 0X639b0da6L, 0X675a1011L,
0X791d4014L, 0X7ddc5da3L, 0X709f7b7aL, 0X745e66cdL,
0X9823b6e0L, 0X9ce2ab57L, 0X91a18d8eL, 0X95609039L,
0X8b27c03cL, 0X8fe6dd8bL, 0X82a5fb52L, 0X8664e6e5L,
0Xbe2b5b58L, 0Xbaea46efL, 0Xb7a96036L, 0Xb3687d81L,
0Xad2f2d84L, 0Xa9ee3033L, 0Xa4ad16eaL, 0Xa06c0b5dL,
0Xd4326d90L, 0Xd0f37027L, 0Xddb056feL, 0Xd9714b49L,
0Xc7361b4cL, 0Xc3f706fbL, 0Xceb42022L, 0Xca753d95L,
0Xf23a8028L, 0Xf6fb9d9fL, 0Xfbb8bb46L, 0Xff79a6f1L,
0Xe13ef6f4L, 0Xe5ffeb43L, 0Xe8bccd9aL, 0Xec7dd02dL,
0X34867077L, 0X30476dc0L, 0X3d044b19L, 0X39c556aeL,
0X278206abL, 0X23431b1cL, 0X2e003dc5L, 0X2ac12072L,
0X128e9dcfL, 0X164f8078L, 0X1b0ca6a1L, 0X1fcdbb16L,
0X018aeb13L, 0X054bf6a4L, 0X0808d07dL, 0X0cc9cdcaL,
0X7897ab07L, 0X7c56b6b0L, 0X71159069L, 0X75d48ddeL,
0X6b93dddbL, 0X6f52c06cL, 0X6211e6b5L, 0X66d0fb02L,
0X5e9f46bfL, 0X5a5e5b08L, 0X571d7dd1L, 0X53dc6066L,
0X4d9b3063L, 0X495a2dd4L, 0X44190b0dL, 0X40d816baL,
0Xaca5c697L, 0Xa864db20L, 0Xa527fdf9L, 0Xa1e6e04eL,
0Xbfa1b04bL, 0Xbb60adfcL, 0Xb6238b25L, 0Xb2e29692L,
0X8aad2b2fL, 0X8e6c3698L, 0X832f1041L, 0X87ee0df6L,
0X99a95df3L, 0X9d684044L, 0X902b669dL, 0X94ea7b2aL,
0Xe0b41de7L, 0Xe4750050L, 0Xe9362689L, 0Xedf73b3eL,
0Xf3b06b3bL, 0Xf771768cL, 0Xfa325055L, 0Xfef34de2L,
0Xc6bcf05fL, 0Xc27dede8L, 0Xcf3ecb31L, 0Xcbffd686L,
0Xd5b88683L, 0Xd1799b34L, 0Xdc3abdedL, 0Xd8fba05aL,
0X690ce0eeL, 0X6dcdfd59L, 0X608edb80L, 0X644fc637L,
0X7a089632L, 0X7ec98b85L, 0X738aad5cL, 0X774bb0ebL,
0X4f040d56L, 0X4bc510e1L, 0X46863638L, 0X42472b8fL,
0X5c007b8aL, 0X58c1663dL, 0X558240e4L, 0X51435d53L,
0X251d3b9eL, 0X21dc2629L, 0X2c9f00f0L, 0X285e1d47L,
0X36194d42L, 0X32d850f5L, 0X3f9b762cL, 0X3b5a6b9bL,
0X0315d626L, 0X07d4cb91L, 0X0a97ed48L, 0X0e56f0ffL,
0X1011a0faL, 0X14d0bd4dL, 0X19939b94L, 0X1d528623L,
0Xf12f560eL, 0Xf5ee4bb9L, 0Xf8ad6d60L, 0Xfc6c70d7L,
0Xe22b20d2L, 0Xe6ea3d65L, 0Xeba91bbcL, 0Xef68060bL,
0Xd727bbb6L, 0Xd3e6a601L, 0Xdea580d8L, 0Xda649d6fL,
0Xc423cd6aL, 0Xc0e2d0ddL, 0Xcda1f604L, 0Xc960ebb3L,
0Xbd3e8d7eL, 0Xb9ff90c9L, 0Xb4bcb610L, 0Xb07daba7L,
0Xae3afba2L, 0Xaafbe615L, 0Xa7b8c0ccL, 0Xa379dd7bL,
0X9b3660c6L, 0X9ff77d71L, 0X92b45ba8L, 0X9675461fL,
0X8832161aL, 0X8cf30badL, 0X81b02d74L, 0X857130c3L,
0X5d8a9099L, 0X594b8d2eL, 0X5408abf7L, 0X50c9b640L,
0X4e8ee645L, 0X4a4ffbf2L, 0X470cdd2bL, 0X43cdc09cL,
0X7b827d21L, 0X7f436096L, 0X7200464fL, 0X76c15bf8L,
0X68860bfdL, 0X6c47164aL, 0X61043093L, 0X65c52d24L,
0X119b4be9L, 0X155a565eL, 0X18197087L, 0X1cd86d30L,
0X029f3d35L, 0X065e2082L, 0X0b1d065bL, 0X0fdc1becL,
0X3793a651L, 0X3352bbe6L, 0X3e119d3fL, 0X3ad08088L,
0X2497d08dL, 0X2056cd3aL, 0X2d15ebe3L, 0X29d4f654L,
0Xc5a92679L, 0Xc1683bceL, 0Xcc2b1d17L, 0Xc8ea00a0L,
0Xd6ad50a5L, 0Xd26c4d12L, 0Xdf2f6bcbL, 0Xdbee767cL,
0Xe3a1cbc1L, 0Xe760d676L, 0Xea23f0afL, 0Xeee2ed18L,
0Xf0a5bd1dL, 0Xf464a0aaL, 0Xf9278673L, 0Xfde69bc4L,
0X89b8fd09L, 0X8d79e0beL, 0X803ac667L, 0X84fbdbd0L,
0X9abc8bd5L, 0X9e7d9662L, 0X933eb0bbL, 0X97ffad0cL,
0Xafb010b1L, 0Xab710d06L, 0Xa6322bdfL, 0Xa2f33668L,
0Xbcb4666dL, 0Xb8757bdaL, 0Xb5365d03L, 0Xb1f740b4L
};

//
//------------------------------------------------------------------------------
// 功 能:CRC32校验码程序
// 输入参数:
// aData:要参与CRC32校验的内容buffer
// aSize:要参与CRC32校验的字节数
// 返 回:
// CRC32校验结果
//------------------------------------------------------------------------------
unsigned long CRC_32( unsigned char * aData, unsigned long aSize )
{
unsigned
long i;
unsigned
long crc32 = 0;//CRC32校验码初值
unsigned long tabitem;

while(aSize--)
{
tabitem
=( crc32 >> 24 )^ *aData++;
crc32
= ( crc32 << 8 ) ^ crctable[tabitem];
}
return crc32;
}

main()
{
unsigned
long result_CRC;
unsigned
char test[512]={0xd8,31,32,33,34,35,36,37,38,39};
unsigned
long i=0;
result_CRC
=CRC_32(test,1);
printf(
"0X%08lxL\n",result_CRC);
}


 
关于种豆 ┊ 联系我们 ┊ 免责声明 ┊ 发帖须知 ┊ 请提意见 ┊ 站点地图
本站为个人爱好兴趣分享网站,不代表本人观点,如有侵权请联系QQ3033380280进行处理
sowsoy.com 版权所有 Copyright©2010-2021 备案号:蜀ICP备2020025376号-3
Email:sowsoy#hotmail.com