搜索CRC算法的C语言代码

作者&投稿:糜莺 (若有异议请与网页底部的电邮联系)
在线等,求一个用C语言写的CRC-16位校验的WIN32控制台程序,非查表法~

#include

typedef unsigned short ushort;
typedef unsigned char uchar;

typedef union _CRC
{
ushort crc16;
uchar by[2];
} CRC;

//输入不带CRC码的数据时,返回值是CRC码
//输入带CRC码的数据时,则可以进行校验,返回0时CRC校验成功,否则CRC校验失败
ushort CRC16(uchar *ba, int size)
{
CRC crc;
crc.crc16 = 0xffff;
int i, l;
for (i=0; i<size; i++)
{
uchar ch = ba[i];
crc.by[0] = crc.by[0] ^ ch;
for (l=0; l<8; l++)
{
if (crc.by[0] & 0x01)
{
crc.crc16 = crc.crc16 >> 1;
crc.crc16 = crc.crc16 ^ 0xa001;
}
else
{
crc.crc16 = crc.crc16 >> 1;
}
}
}
uchar swap = crc.by[0];
crc.by[0] = crc.by[1];
crc.by[1] = swap;
return crc.crc16;
}

void main()
{
uchar ba[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
CRC crc;
//计算CRC码
crc.crc16 = CRC16(ba, 8);
printf("高字节:0x%x, 低字节:0x%x
", crc.by[1], crc.by[0]);
//CRC校验
uchar bb[10] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0xb0, 0xcf};
if (0 == CRC16(bb, 10))
{
printf("bb 校验成功!");
}
else
{
printf("bb 校验失败!");
}
}

折半查找
#include
#define N 51
void main(void)
{
int a[N];
int i,n,num;
int top,bottom,mid;
int flag=1; //如果在表列中找到数字,则值为1,否则为0
int loc=-1;//要查找的数在表列中的位置,如果loca=-1表示表列中没有这个数;如果有这个数,则它的值为所在的位置

printf("你想在多少个数中进行折半查找,请输入(1--50):");
scanf("%d",&n);

while(n50)
{
printf("你输入的数不正确,请重新输入。
");
printf("你想在多少个数中进行折半查找,请输入(1--50):");
scanf("%d",&n);
}

printf("请你输入一个整数 a[1](需保证递增有序):");
scanf("%d",&a[1]);

i=2;
while(i<=n) //输入从小到大的表列
{
printf("请你输入一个整数 a[%d](需保证递增有序):",i);
scanf("%d",&a[i]);
if(a[i] > a[i-1])
i++;
else
printf("你输入的数不满足要求,请重新输入。
");
}

//输出表列
printf("
输出表列
");
for(i=1; i<=n; i++)
{
printf("%6d",a[i]);
}
printf("
");

printf("请你输入要查找的数:");
scanf("%d",&num);

flag=1; //假设输入的数在表列中

top=n;
bottom=1;
mid=(top+bottom)/2;

while(flag)
{

//printf("top=%d, bottom=%d, mid=%d, a[%d]=%d
",top,bottom,mid,mid,a[mid]);

if( (num>a[top]) || (numa[top] 或者 num<a[bottom],肯定num不在这个表列中
{
loc=-1;
flag=0;
}
else if(a[mid]==num) //如果num 等于找到的数
{
loc=mid;
printf("找到数 %6d 的位置%2d
",num,loc);
break;
}
else if(a[mid]>num) //若 a[mid]>num,则num 一定在 a[bottom]和a[mid-1]范围之内
{
top=mid-1;
mid=(top+bottom)/2;
}
else if(a[mid]<num) //若 a[mid]<num,则num 一定在 a[mid+1]和a[top]范围之内
{
bottom=mid+1;
mid=(top+bottom)/2;
}
}

if(loc==-1)
{
printf("%d 这个数在表列中没有找到。
",num);
}
printf("折半查找结束,按任意键退出:
");

}

#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);
}

给你一篇参考文章,既有理论,又有代码:

http://www.yuanma.org/data/2006/1010/article_1637.htm
或者
http://www.mcu-club.com/upload/2009112715521857532.pdf
或者
http://space.itpub.net/672726/viewspace-543089

可以google一下,哈哈


C语言编程(要发送的数据为101110,采用CRC的生成多项式是P(X)=Xe3...
发送的数是:0x2E 这个e要精确的么?

关于c语言中16位数据的处理?
显然buffer是字节类型的数组,将高字节乘以256(左移8位)与低字节相加,得到一个16位的整数,这个就是CRC的长度。

把下面这段c语言的crc校验 转换成java的,麻烦了, 我是实在不会_百度知...
unsigned short 都替换为int unsigned int 也替换为int unsigned char const *buf替换为byte[] buf buf++替换为buf[i]

关于CRC算法,高手赐教
在这种情况下 CRC-32 几乎同 CRC-40 一样优秀。===设计 CRC 多项式===生成多项式的选择是 CRC 算法实现中最重要的部分,所选择的多项式必须有最大的错误检测能力,同时保证总体的碰撞概率最小。多项式最重要的属性是它的长度,也就是最高非零系数的数值,因为它直接影响着计算的校验和的长度。最常用的多项式长度...

CRC原理与快速Verilog仿真
如 POLY=10011;输入DATA=11100110;CRC的演算:硬件实现算法可以先load 4bit data到crc_sft逻辑,然后做8次循环,每次data左移1bit数据到crc_sht,如果crc_sft最高位为1,则crc_sft和poly做异或运算;make run case=crc4_1 测试结果:遇到bit类的运算等和数字硬件强烈相关的操作,总感觉使用C或...

rfid技术标准采用了哪些crc检验算法
一般用CRC16的C语言算法:define PRESET_VALUE 0xFFFF define POLYNOMIAL 0x8408 unsigned int uiCrc16Cal(unsigned char const * pucY, unsigned char ucX){ unsignedchar ucI,ucJ;unsignedshort int uiCrcValue = PRESET_VALUE;for(ucI = 0; ucI < ucX; ucI++){ uiCrcValue = uiCrcValue ...

怎样在vb,c,delphi,汇编语言中使用crc算法
在Delphi XE3中,Indy组件中已经有CRC验证函数了,可以直接使用。 uses IdHashCRC; var s:String; s:=IntToStr(GetStringCRC('010303020014')); ShowMessage(s);

st语言和c语言哪个难
而C语言是一种广泛应用的通用编程语言,设计目的是为了达到高效的编程效率,支持过程式程序设计、结构化程序设计、模块化程序设计等多种程序设计风格。语言特性:ST语言能处理复杂的数字方程,具有极高的运算速度和效率,尤其适用于处理一些需要大量计算的复杂问题,如CRC校验、复杂浮点数运算、多项式函数运算、...

求modbus 通讯 crc校验代码
\/* CRC低位字节值表*\/ static char auchCRCLo[]={ 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,0x08, ...

如何用易语言crc8
所以在多项式记录时都去掉了最高位。CRC校验算法,说白了,就是把需要校验的数据与多项式进行循环异或(XOR),但进行XOR的方式与实际中数据传输时,是高位先传、还是低位先传有关。对于数据 高位先传的方式,XOR从数据的高位开始,我们就叫它顺序异或吧;对于数据低位先 传的方式,XOR从数据的低位开始...

故城县18397397232: 用C语言实现CRC编码程序 -
贺进重感: #include <stdio.h> #include <string.h> #include "stdlib.h" unsigned int char2int(char *str) {unsigned int count=0, ret=0;for(count = 0; count<strlen(str);count++){ret = ret<<1;if('0' != str[count]){ ret+=1;}}return ret; }unsigned int getR(char *str...

故城县18397397232: 求crc完整代码,急急急急急 -
贺进重感: public class myCrc32 {static CRC32 crc=new CRC32();static HashMap<String,String> map=new HashMap<String,String>();public static String getCrc32(String s){if(map.containsKey(s))return map.get(s);crc.reset();crc.update(s.getBytes()); ...

故城县18397397232: C语言 编写一个程序 求输入一个数计算它的CRC值, -
贺进重感: unsigned int crc(unsigned char *buf,unsigned char len) {char i;unsigned int c_dat=0xffff;//有的CRC校验这个值是0for (;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++;}return c_dat; }

故城县18397397232: 求C++中求CRC码的程序代码 -
贺进重感: unsigned char crc8(unsigned char *source,unsigned int number) { unsigned int i = 0; unsigned char sumvalue = source[0]; for(i = 1; i{ sumvalue ^= source[i]; } return sumvalue; }

故城县18397397232: C语言零基础,怎么用C语言实现CRC16检验码 -
贺进重感: 1.系统先把所有的float转换为double类型运算,最终得到的结果截取前七个作为有效数字,这样做可以使计算结果更准确. 2.有效数字:从左边第一个不是0的数字起,到精确到的位数止,所有的数字都叫做这个数的有效数字.比如:1.24的有效...

故城县18397397232: 计算crc的完整程序 -
贺进重感: 我给你delphi的CRC算法,这个文件可以直接使用unit Main;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls;typeTForm1 = class(TForm)Memo1: TMemo;Label1: TLabel;Label2: TLabel; ...

故城县18397397232: 我要用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) 另外,站长团上有产品团购,便宜有保证

故城县18397397232: C语言中CRC循环校验的一个程序 -
贺进重感: while(len--!=0) 这句的len的值循环一次就减少1,先执行len!=0,再执行len--.当len为0时退出循环.for(i=0x80; i!=0; i/=2) 0x80是十六进制数,也即128 当i!=0时,执行循环体,然后i=i/2,即i值减半.

故城县18397397232: C++一个crc的算法求 -
贺进重感: 这个函数的参数和变量命名还是很清楚的表明了其意义, ptr 应该指向的是一个数组, len 应该是数组的长度, 函数返回一个 unsigned short 的校验值. 下面是测试代码:#include "stdio.h" int main() { unsigned char data[] = {0x31, 0x32, 0x33, 0x34}; printf("crc:%x",crc16(data,4)); return 0; }

故城县18397397232: 求一个C# CRC8 的算法代码 生成多项式可以随意选 能够得到CRC码 然后调用函数 -
贺进重感: public static byte CRC8(byte[] buffer,byte poly){byte crc = 0;byte CRCPoly = poly;for (int j = 0; j < buffer.Length; j++){crc ^7a686964616fe59b9ee7ad9431333431373866= buffer[j];for (int i = 0; i < 8; i++){if ((crc & 0x80) != 0){crc <<= 1;crc ...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网