crc计算c语言
A. crc16校验的c语言程序
unsigned short crc_dsp(unsigned short reg, unsigned char data_crc)
//reg为crc寄存器, data_crc为将要处理的8bit数据流
{
unsigned short msb; //crc寄存器将移出的最高1bit
unsigned short data;
unsigned short gx = 0x8005, i = 0; //i为左移次数, gx为生成多项式
data = (unsigned short)data_crc;
data = data << 8;
reg = reg ^ data;
do
{
msb = reg & 0x8000;
reg = reg << 1;
if(msb == 0x8000)
{
reg = reg ^ gx;
}
i++;
}
while(i < 8);
return (reg);
}
B. 搜索CRC算法的C语言代码
#include "stdio.h"
unsigned short CRC16( unsigned char *data, int length)
{
unsigned short reg_crc;
unsigned short s_crcchk;
// CString crc;
s_crcchk = 0;
reg_crc = 0x0; //应该是0,不是ffff
while(length--)
{
reg_crc ^= *data++;
for(s_crcchk = 0; s_crcchk < 8; s_crcchk ++)
{
//reg_crc=reg_crc>>1;
if(reg_crc & 0x01)
{
reg_crc = (reg_crc >> 1)^0xa001;//这里填多项式
}
else
{
reg_crc = reg_crc >> 1;
}
}
}
return reg_crc;
}
void main()
{
unsigned char a[8]={0xCD,0x67,0x41,0x85,0x00,0x00,0x00,0x01};//这里填数据
int me=CRC16(a,8);
printf("%x",me);
}
C. CRC32的计算方法
CRC的本质是模-2除法的余数,采用的除数不同,CRC的类型也就不一样。通常,CRC的除数用生成多项式来表示。 最常用的CRC码及生成多项式名称生成多项式。
CRC-12:
(3)crc计算c语言扩展阅读
通常的CRC算法在计算一个数据段的CRC值时,其CRC值是由求解每个数值的CRC值的和对CRC寄存器的值反复更新而得到的。这样,求解CRC的速度较慢。通过对CRC算法的研究,我们发现:一个8位数据加到16位累加器中去,只有累加器的高8位或低8位与数据相作用,其结果仅有256种可能的组合值。
因而,我们可以用查表法来代替反复的运算,这也同样适用于CRC32的计算。本文所提供的程序库中,函数crchware是一般的16位CRC的算法。mk-crctbl用以在内存中建立一个CRC数值表。
D. 高分求计算CRC校验码的C语言程序
你就是想要CRC8-CCITT的代码,这个到处都是。
http://www.rajivchakravorty.com/source-code/uncertainty/multimedia-sim/html/crc8_8c-source.html
我一直有CRC16,没试过这个,但应该差不多。
参考文献:http://blog.sina.com.cn/s/blog_5e330a280100fcp9.html
E. 我要用C语言编写CRC16效验码。通过输入一个串然后计算出CRC16效验码。例如:输入010600001388计算得到849C
int cal_crc(unsigned char *ptr, unsigned char len)
{
unsigned char i;
unsigned int crc_value =0;
while(len--)
{
for(i=0x80; i!=0; i>>=1 )
{
if (crc_value&0x8000)
crc_value = (crc_value << 1) ^0x8005 ;
else
crc_value = crc_value << 1 ;
if(*ptr&i)
另外,站长团上有产品团购,便宜有保证
F. CRC C语言实现,谁帮我讲讲是什么原理。
参考:http://bbs.pediy.com/showthread.php?t=17195
G. CRC的C语言的程序
按位计算CRC采用CRC-CCITT多项式,多项式为0x11021,C语言编程时,参与计算为0x1021。当按位计算CRC时,例如计算二进制序列为1001 1010 1010 1111时,将二进制序列数左移16位,即为1001 1010 1010 1111 (0000 0000 0000 0000),实际上该二进制序列可拆分为1000 0000 0000 0000 (0000 0000 0000 0000) + 000 0000 0000 0000 (0000 0000 0000 0000) + 00 0000 0000 0000 (0000 0000 0000 0000) + 1 0000 0000 0000 (0000 0000 0000 0000) + ……
现在开始分析运算:
<1>对第一个二进制分序列求余数,竖式除法即为0x10000 ^ 0x11021运算,后面的0位保留;
<2>接着对第二个二进制分序列求余数,将第一步运算的余数*2后再和第二个二进制分序列一起对0x11021求余,这一步理解应该没什么问题。如果该分序列为0,无需计算。
<3>对其余的二进制序列求余与上面两步相同。
<4>计算到最后一位时即为整个二进制序列的余数,即为CRC校验码。
该计算方法相当于对每一位计算,运算过程很容易理解,所占内存少,缺点是一位一位计算比较耗时。
下面给出C语言实现方法:
代码如下:
unsigned char test[16] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff};
unsigned char len = 16;
void main( void )
{
unsigned long temp = 0;
unsigned int crc;
unsigned char i;
unsigned char *ptr = test;
while( len-- ) {
for(i = 0x80; i != 0; i = i >> 1) {
temp = temp * 2;
if((temp & 0x10000) != 0)
temp = temp ^ 0x11021;
if((*ptr & i) != 0)
temp = temp ^ (0x10000 ^ 0x11021);
}
ptr++;
}
crc = temp;
printf("0x%x ",crc);
}
H. C语言 编写一个程序 求输入一个数计算它的CRC值,
unsignedintcrc(unsignedchar*buf,unsignedcharlen)
{
chari;
unsignedintc_dat=0xffff;//有的CRC校验这个值是0
for(;len>0;len--)//
{
c_dat^=*buf;
for(i=0;i<8;i++)
{
if((c_dat&0x1)==0x1)
{
c_dat>>=1;
c_dat^=0xa001;
}
else
{
c_dat>>=1;
}
}
buf++;
}
returnc_dat;
}
I. c语言 CRC的检验方式 我想问一下。这下面的C语言返回的CRC的值是什么。他有他的公式是怎么样的
CRC又称循环冗余校验,CRC返回的值其实是校验位,校验位分高位和低位。
实际应用时,发送装置计算出CRC值并随数据一同发送给接收装置,接收装置对收到的数据重新计算CRC并与收到的CRC相比较,若两个CRC值不同,则说明数据通讯出现错误。
J. 大侠给我个完整的crc 程序 用C语言实现的,能在PC机上运行的。我马上拿出100分。。。
楼主程序中调用函数cal_crc()的方式不正确。函数cal_crc()用于计算输入串的校验码,因此函数输入参数包含输入串及该串的长度。
建议将main()函数修改为:
#include <string.h>
void main()
{
unsigned char buf[] = "ABCDEFG1234567"; // 输入串
unsigned char len = 14; // 输入串的长度
unsigned int crc;
crc = cal_crc(buf, len);
}
最后需要注意的是,输入串的长度不能大于256个字节。上述例子程序中假定了输入串为字符串,实际上,还可以是字节串,此时变量len表示字节串的包含的字节个数。