crc16校验码在线计算 crc16校验码计算工具

2025-04-05 03:08 - 立有生活网

CRC-16计算样例(01 06 01 0E 00 64)的CRC值是多少?

#include

crc16校验码在线计算 crc16校验码计算工具crc16校验码在线计算 crc16校验码计算工具


crc16校验码在线计算 crc16校验码计算工具


#define CRC_INIT 0xffff //CCITT初始CRC为全1

#define GOOD_CRC 0xf0b8 //校验时计算出的固定结果值

/下表是常用ccitt 16,生成式1021反转成8408后的查询表格/

unsigned short crc16_ccitt_table[256] =

{0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,

0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,

0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,

0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,

0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,

0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,

0x3183, 0x200a, 0x12, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,

0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,

0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,

0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,

0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,

0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,

0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,

0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,

0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,

0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,

0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,

0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,

0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,

0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,

0xa50a, 0xb483, 0x8618, 0x97, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,

0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,

0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,

0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,

0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0xa1, 0xa33a, 0xb2b3,

0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,

0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,

0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,

0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,

0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,

0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,

0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78

};

/CRC计算函数,可将个参数reg_init简化掉/

unsigned short do_crc(unsigned short reg_init, unsigned char message, unsigned int len)

{unsigned short crc_reg = reg_init;

while (len--)

crc_reg = (crc_reg >> 8) ^ crc16_ccitt_table[(crc_reg ^ message++) & 0xff];

return crc_reg;

}/测试,发送和接收用同一个数组模拟/

int main() {

unsigned char p[]= {0xa0,0xb0,0xff, 0xff};

unsigned short crc;

crc= do_crc(0xffff, p, 2); //计算前两位的CRC码

crc^=0xffff; //对其取反,实际应用中,发送端一定要记着这一步!!!

p[2]=crc&0x00ff; //将计算的CRC码加到信息序列后面

p[3]=crc>>8&0x00ff; //封装成代CRC码的发送数组缓冲

printf("p[2]=%x,p3=%xn",p[2],p[3]);

crc=do_crc(0xffff,p,4);//对信息码+CRC码共同计算得出CRC=0xf0b8,设接收端就是这些内容

printf("crc is %xn",crc);

if(crc==GOOD_CRC) printf("check ok!n");

return 0;

}

如何使用Delphi编写Modbus RTU CRC16的校验码

在工业控制中,Modbus RTU CRC16的校验码用的比较广泛,包括本人富士产品中,PC与伺服电机以及PC与VP系列的变频器的Modbus RTU通讯中都使用到了CRC16.

而对CRC16的计算的方式基本上有2种:种,使用双循环依照CRC的计算方法进行计算,第二种,采用查表的方式。本人愚钝无比,从网络上搜来的查表法都与实际的正确CRC16的结果有所异,因此编写了一个小程序供自己使用。

软件的界面很简单,输入诸如“010303020014”的值,然后每2个字符作为一个字节,填入字节数,然后就可以计算出校验码,校验码的多项式为:X16+X15+X2+1.

实现的源代码如下:

unit Unit1;

intece

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

type

TForm1 = class(TForm)

Edit1: TEdit;

Button1: TButton;

Edit2: TEdit;

Edit3: TEdit;

Label1: TLabel;

Label2: TLabel;

Label3: TLabel;

Memo1: TMemo;

Label4: TLabel;

function CalCRC16(AData:array of Byte;AStart,AEnd:Integer):Word;

procedure Button1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R .dfm}

//××××××××××××××××××××××××××

// CalCRC16用于计算Modbus RTU的CRC16

// 多项式公式为X16+X15+X2+1

//××××××××××××××××××××××××××

function TForm1.CalCRC16(AData:array of Byte;AStart,AEnd:Integer):Word;

const

GENP=$A001; //多项式公式X16+X15+X2+1(1100 0000 0000 0101)

var

crc:Word;

i:Integer;

tmp:Byte;

procedure CalOneByte(AByte:Byte); //计算1个字节的校验码

var

j:Integer;

begin

crc:=crc xor AByte; //将数据与CRC寄存器的低8位进行异或

for j:=0 to 7 do //对每一位进行校验

begin

tmp:=crc and 1; //取出位

crc:=crc shr 1; //寄存器向右移一位

crc:=crc and $7FFF; //将位置0

if tmp=1 then //检测移出的位,如果为1,那么与多项式异或

crc:=crc xor GENP;

crc:=crc and $FFFF;

end;

end;

begin

crc:=$FFFF; //将余数设定为FFFF

for i:=AStart to AEnd do //对每一个字节进行校验

CalOneByte(AData[i]);

Result:=crc;

end;

procedure TForm1.Button1Click(Sender: TObject);

var

Data:array[0..255] of Byte;

i,j,Count:Integer;

Res:Word;

szData:string;

begin

szData:=Form1.Edit2.Text; //读入欲校验的字符串

Count:=StrToInt(form1.Edit3.Text); //读入需要计算的字符串长度

i:=1;

j:=0;

for j:=0 to Count-1 do

begin

if (i mod 2)=0 then //每2个字符放入一个字节中

i:=i+1;

if i>=Length(szData) then

exit;

Data[j]:=StrToInt('$'+copy(szData,i,2)); //取出字符并转换为16进制数

i:=i+1;

end;

Res:=CalCRC16(Data,Low(Data),Count-1);

form1.Edit1.Text:=IntToHex(Res,4);

end;

end.

C#写一个CRC16 MODBUS校验

参考如下: using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.Serialization.Formatters.Binary;

using System.IO; namespace sForCsharp.CRC

{///

/// 消息CRC校验算法

///

public class CRC

{ public static String getCrc16Code(String crcString)

{ // 转换成字节数组

byte[] creBytes = HexString2Bytes(crcString); // 开始crc16校验码计算

CRC16Util crc16 = new CRC16Util();

crc16.reset();

crc16.update(creBytes);

int crc = crc16.getCrcValue();

// 16进制的CRC码

String crcCode = Convert.ToString(crc, 16).ToUpper();

// 补足到4位

if (crcCode.Length < 4)

{// crcCode = StringUtil.lefgPadding(crcCode, '0', 4);

crcCode = crcCode.PadLeft(4, '0');

}return crcCode;

} public static String RealHexToStr(String str)

{String hText = "0123456789ABCDEF";

StringBuilder bin = new StringBuilder();

for (int i = 0; i < str.Length; i++)

{bin.Append(hText[str[i] / 16]).Append(hText[str[i] % 16]).Append(' ');

}return bin.ToString();

}/

十六进制字符串转换成字节数组

@param hexstr

@return

/

public static byte[] HexString2Bytes(String hexstr)

{byte[] b = new byte[hexstr.Length / 2];

int j = 0;

for (int i = 0; i < b.Length; i++)

{char c0 = hexstr[j++];

char c1 = hexstr[j++];

b[i] = (byte)((parse(c0) << 4) | parse(c1));

}return b;

} /

16进制char转换成整型

@param c

@return

/

public static int parse(char c)

{if (c >= 'a')

return (c - 'a' + 10) & 0x0f;

if (c >= 'A')

return (c - 'A' + 10) & 0x0f;

return (c - '0') & 0x0f;

} public static string ByteArrayToHexString(byte[] data)//字节数组转为十六进制字符串

{StringBuilder = new StringBuilder(data.Length 3);

foreach (byte b in data)

.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));

return .ToString().ToUpper();

} }

public class CRC16Util

{ /CRC值/

private int value = 0xffff; private static int[] CRC16_TABLE = {

0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,

0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,

0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,

0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,

0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,

0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,

0x3183, 0x200a, 0x12, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,

0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,

0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,

0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,

0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,

0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,

0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,

0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,

0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,

0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,

0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,

0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,

0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,

0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,

0xa50a, 0xb483, 0x8618, 0x97, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,

0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,

0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,

0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,

0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0xa1, 0xa33a, 0xb2b3,

0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,

0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,

0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,

0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,

0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,

0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,

0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78

}; /

计算一个字节数组的CRC值

@param data

/

public void update(byte[] data)

{//int fcs = 0xffff;

for (int i = 0; i < data.Length; i++)

{// 1.value 右移8位(相当于除以256)

// 2.value与进来的数据进行异或运算后再与0xFF进行与运算

// 得到一个索引index,然后查找CRC16_TABLE表相应索引的数据

// 1和2得到的数据再进行异或运算。

value = (value >> 8) ^ CRC16_TABLE[(value ^ data[i]) & 0xff];

}// 取反

//return ~fcs;

} /

计算一个byte的CRC值

@param aByte

/

public void update(byte aByte)

{value = (value >> 8) ^ CRC16_TABLE[(value ^ aByte) & 0xff];

} /

重新设定CRC初始值

/

public void reset()

{value = 0xffff;

} /

获取计算好的CRC值

@return

/

public int getCrcValue()

{return ~value & 0xffff;

} ///

/// 生成FCS校验值

///

///

///

public static byte[] makeCrc16(byte[] ccc)

{ CRC16Util crc16 = new CRC16Util();

crc16.reset();

crc16.update(ccc);

//Console.WriteLine(RealHexToStr(crc16.getCrcValue().ToString()));

byte[] test = intToByte(crc16.getCrcValue());

//log(RealHexToStr(crc16.getCrcValue().ToString()));

return test; }

private static int[] copy(byte[] aa)

{int[] cc = new int[aa.Length];

for (int i = 0; i < aa.Length; i++)

{cc[i] = aa[i];

}return cc;

} public static byte[] intToByte(int i)

{byte[] abyte0 = new byte[4];

abyte0[0] = (byte)(0xff & i);

abyte0[1] = (byte)((0xff00 & i) >> 8);

abyte0[2] = (byte)((0xff0000 & i) >> 16);

abyte0[3] = (byte)((0xff000000 & i) >> 24);

return abyte0;

} private static void log(Object obj)

{//(obj);

} private static void printBytes(byte[] bytes)

{for (int i = 0; i < bytes.Length; i++)

{//System.out.print(bytes[i]+ "-");

}} public static String RealHexToStr(String str)

{String hText = "0123456789ABCDEF";

StringBuilder bin = new StringBuilder();

for (int i = 0; i < str.Length; i++)

{bin.Append(hText[str[i] / 16]).Append(hText[str[i] % 16]).Append(' ');

}return bin.ToString();

} }

}

crc校验码计算方法

已知信息位为1100,生成多项式G(x) = x3+x+1,求CRC码。

M(x) = 1100 M(x)x3 = 1100000 G(x) = 1011

M(x)x3 / G(x) = 1110 + 010 /1011 R(x) = 010

CRC码为: M(x)x 3+R(x)=1100000+010 =1100010

其原理是:CRC码一般在k位信息位之后拼接r位校验位生成。编码步骤如下:

(1)将待编码的k位信息表示成多项式 M(x)。

(2)将 M(x)左移 r 位,得到 M(x)xr 。

(3)用r+1位的生成多项式G(x)去除M(x)xr 得到余数R(x)。

(4)将M(x)xr 与R(x)作模2加,得到CRC码。

扩展资料:

CRC校验码计算详解:采用CRC进行错检验,生成多项式为G(X)=X4+X+1,信息码字为10110,则计算出的CRC校验码是:A. 0000 B. 0100 C. 0010 D.1111

符号表示定:多项式和多项式的系数排列均用相同的符号表示,如

G(X)= X4+X+1

G(X)=10011

已知条件如下:

原码字记做M(X),即:M(X) = 10110

生成多项式记做G(X),即:G(X) = 10011

G(X)的阶数记做r,此处r = 4

求用matlab生成16位信息位的crc校验码,尽量简单点,怕看不懂

data=randi(2,1,16)-1; %随机16位0,1数据

g=[1 0 0 1 1];%生成多项式g(x)=x4+x+1,crc-4这个简单会产生4位冗余码

R=length(g)-1; %冗余码长为生成多项式长度减1

[q,r] = deconv([data zeros(1,R)],g);

%为数据右边补K个0,然后用deconv计算数据多项式除以生成多项式

%商是q(长度16),余数是r(长度16+R)

r=mod(r(end-R+1:end),2); %取余数的R位mod2运算

code=[data r]; %编码是原来的16位数据后加上R位校验冗余码

%发送的信号就是这16+R位二进制编码 %验证:将接收到的16+R位编码和生成多项式相除

[q,r] = deconv(code,g);

r=mod(r(end-R+1:end),2);

disp(r) %如果正确,R位余数全是0

errorcode=code;

errorcode(2)=1-errorcode(2); %设第二位错了

[q,r] = deconv(errorcode,g);

r=mod(r(end-R+1:end),2);

disp(r) %R位余数全为0

检验结果

0 0 0 0

1 0 0 0

表示结果次正确

第二次有误

CRC检查(16位),

CRC校验,你首先要把信息看成是比特流,就是由位(bit)组成的信息,而不要有字节的概念,1~16共计有16字节吧也就是168共计128个bit,也就是信息码长度为k=128,R=16,

求助!!!CRC校验计算问题!

(1)应传输的信息为101100100100101111000100

(2)接收方会用收到的消息除以CRC多项式X^8+X^2+X+1,如果余式为不为0,则说明发生了错。或用前16位信息码元来算CRC校验码,如果算出来的校验码和发过来的校验码不一样也说明发生了错。

王亚平比刘洋漂亮 刘洋和王亚平谁的职务高低

从普通山东农村女孩到两次登陆太空,女航天员王亚平到底有多飒? 神舟十三号载人飞船成功发射,而此次王亚平也将成为首位实施出舱活动的女航天员,可以说具有划时代的意义。 王亚平比刘洋···

天赐的声音阿信 天赐的声音阿信退出

信在《天赐3》因“比耶”而遭乐评人犀利点评,信是如何回应他的点评的? 信表示自己不会对对这件事情发表任何的言论,也表示乐评人说的对,但是可以看出来一直都在忍着自己的怒火。 天赐···

txt打开乱码(txt打开乱码怎么解决)

txt打开乱码(txt打开乱码怎么解决) txt打开乱码(txt打开乱码怎么解决) 关于txt打开乱码,txt打开乱码怎么解决这个很多人还不知道,今天怡怡来为大家解答以上的问题,现在让我们一起来看看吧! ···