CRC16问题(顺便帮忙校验一下计算结果)(100分)

  • 主题发起人 主题发起人 我无言
  • 开始时间 开始时间

我无言

Unregistered / Unconfirmed
GUEST, unregistred user!
请各位大哥大姐帮小弟算一下以下的字符串的CRC16的校验码(最好提供函数和调用方法),我自己用不同的函数算出来又几个结果!!
多项式:X16+X15+X2+1(IBM的),$8500
要校验的字符串 计算结果
1b10610007(当成字符串) 0830或FCD8

1b10610007(当成16进制$1b$10$61$00$07) 2FBC或E305

100 分好礼相送
 
麻烦各位热情相助!!!
 
我这有一段比较文件的,但用的是 CRC32
Unit Crc32; //这是CRC 的 Unit,调用这个就可以了
interface

uses Dialogs, SysUtils, Windows;

type
CRCTable = Array[0..255] of LongInt; //建立一个 CRC 表

const
BufferLength = 32768; //缓存
//以下是详细的 CRC 表
CRC32Table: CRCTable = (
$000000000, $077073096, $0ee0e612c, $0990951ba,
$0076dc419, $0706af48f, $0e963a535, $09e6495a3,
$00edb8832, $079dcb8a4, $0e0d5e91e, $097d2d988,
$009b64c2b, $07eb17cbd, $0e7b82d07, $090bf1d91,

$01db71064, $06ab020f2, $0f3b97148, $084be41de,
$01adad47d, $06ddde4eb, $0f4d4b551, $083d385c7,
$0136c9856, $0646ba8c0, $0fd62f97a, $08a65c9ec,
$014015c4f, $063066cd9, $0fa0f3d63, $08d080df5,

$03b6e20c8, $04c69105e, $0d56041e4, $0a2677172,
$03c03e4d1, $04b04d447, $0d20d85fd, $0a50ab56b,
$035b5a8fa, $042b2986c, $0dbbbc9d6, $0acbcf940,
$032d86ce3, $045df5c75, $0dcd60dcf, $0abd13d59,

$026d930ac, $051de003a, $0c8d75180, $0bfd06116,
$021b4f4b5, $056b3c423, $0cfba9599, $0b8bda50f,
$02802b89e, $05f058808, $0c60cd9b2, $0b10be924,
$02f6f7c87, $058684c11, $0c1611dab, $0b6662d3d,

$076dc4190, $001db7106, $098d220bc, $0efd5102a,
$071b18589, $006b6b51f, $09fbfe4a5, $0e8b8d433,
$07807c9a2, $00f00f934, $09609a88e, $0e10e9818,
$07f6a0dbb, $0086d3d2d, $091646c97, $0e6635c01,

$06b6b51f4, $01c6c6162, $0856530d8, $0f262004e,
$06c0695ed, $01b01a57b, $08208f4c1, $0f50fc457,
$065b0d9c6, $012b7e950, $08bbeb8ea, $0fcb9887c,
$062dd1ddf, $015da2d49, $08cd37cf3, $0fbd44c65,

$04db26158, $03ab551ce, $0a3bc0074, $0d4bb30e2,
$04adfa541, $03dd895d7, $0a4d1c46d, $0d3d6f4fb,
$04369e96a, $0346ed9fc, $0ad678846, $0da60b8d0,
$044042d73, $033031de5, $0aa0a4c5f, $0dd0d7cc9,

$05005713c, $0270241aa, $0be0b1010, $0c90c2086,
$05768b525, $0206f85b3, $0b966d409, $0ce61e49f,
$05edef90e, $029d9c998, $0b0d09822, $0c7d7a8b4,
$059b33d17, $02eb40d81, $0b7bd5c3b, $0c0ba6cad,

$0edb88320, $09abfb3b6, $003b6e20c, $074b1d29a,
$0ead54739, $09dd277af, $004db2615, $073dc1683,
$0e3630b12, $094643b84, $00d6d6a3e, $07a6a5aa8,
$0e40ecf0b, $09309ff9d, $00a00ae27, $07d079eb1,

$0f00f9344, $08708a3d2, $01e01f268, $06906c2fe,
$0f762575d, $0806567cb, $0196c3671, $06e6b06e7,
$0fed41b76, $089d32be0, $010da7a5a, $067dd4acc,
$0f9b9df6f, $08ebeeff9, $017b7be43, $060b08ed5,

$0d6d6a3e8, $0a1d1937e, $038d8c2c4, $04fdff252,
$0d1bb67f1, $0a6bc5767, $03fb506dd, $048b2364b,
$0d80d2bda, $0af0a1b4c, $036034af6, $041047a60,
$0df60efc3, $0a867df55, $0316e8eef, $04669be79,

$0cb61b38c, $0bc66831a, $0256fd2a0, $05268e236,
$0cc0c7795, $0bb0b4703, $0220216b9, $05505262f,
$0c5ba3bbe, $0b2bd0b28, $02bb45a92, $05cb36a04,
$0c2d7ffa7, $0b5d0cf31, $02cd99e8b, $05bdeae1d,

$09b64c2b0, $0ec63f226, $0756aa39c, $0026d930a,
$09c0906a9, $0eb0e363f, $072076785, $005005713,
$095bf4a82, $0e2b87a14, $07bb12bae, $00cb61b38,
$092d28e9b, $0e5d5be0d, $07cdcefb7, $00bdbdf21,

$086d3d2d4, $0f1d4e242, $068ddb3f8, $01fda836e,
$081be16cd, $0f6b9265b, $06fb077e1, $018b74777,
$088085ae6, $0ff0f6a70, $066063bca, $011010b5c,
$08f659eff, $0f862ae69, $0616bffd3, $0166ccf45,

$0a00ae278, $0d70dd2ee, $04e048354, $03903b3c2,
$0a7672661, $0d06016f7, $04969474d, $03e6e77db,
$0aed16a4a, $0d9d65adc, $040df0b66, $037d83bf0,
$0a9bcae53, $0debb9ec5, $047b2cf7f, $030b5ffe9,

$0bdbdf21c, $0cabac28a, $053b39330, $024b4a3a6,
$0bad03605, $0cdd70693, $054de5729, $023d967bf,
$0b3667a2e, $0c4614ab8, $05d681b02, $02a6f2b94,
$0b40bbe37, $0c30c8ea1, $05a05df1b, $02d02ef8d);

function UpdateCRC32(InitCRC: LongInt; Buffer: Pointer; BufferLength: WORD): LongInt;
function ComputeFileCRC32(FileName: String): LongInt;

implementation

var
Buffer: Array[1..BufferLength] of Byte;

//取得一段缓存的 function 不好意思,黑手是外国长大的中文不好
function UpdateCRC32(InitCRC: LongInt; Buffer: Pointer; BufferLength: WORD): LongInt;
var
crc: LongInt;
index: integer;
i: integer;
begin
crc := InitCRC; //一开始的 bit
for i := 0 to BufferLength-1 do
begin
//利用表来算出 CRC 的 bit
index := (crc xor Integer(Pointer(DWORD(Buffer)+i)^)) and $000000FF;
crc := ((crc shr 8) and $00FFFFFF) xor CRC32Table[index];
end;
Result := crc; //返回本段的 bit
end;

//取得一个文件的 CRC 码
function ComputeFileCRC32(FileName: String): LongInt;
var
InputFile: File;
Crc32: LongInt;
ResultLength: Integer;
BufPtr: Pointer;
begin
BufPtr := @Buffer; //认准一段 Pointer
Assign(InputFile, FileName); //开始进入文件
Reset(InputFile, 1);
Crc32 := $FFFFFFFF; { 开始时全部的 bit 都时 on 的,时 CRC32 的专利 }
Repeat //循环读取文件入缓存,直到文件结束
BlockRead(InputFile, Buffer, BufferLength, ResultLength); //取得一段Buffer
Crc32 := UpdateCrc32(Crc32, BufPtr, ResultLength); //取得这一段的 CRC 码
Until Eof(InputFile);
Close(InputFile); //关闭文件
Crc32 := Not(Crc32); { CRC32 标准,反转 CRC 编码 }
Result := CRC32; //回复结果
end;

end.
 
TO 黑手
我手里面的算法和你的差不多,我想校验一下我的CRC16(IBM)的计算结果,确认一下是否正确,谢谢你了!
 
我来帮你算一下,如果你的算法和我一样的话,算出来结果应该时一样
 
1b10610007(当成16进制$1b$10$61$00$07) 是否为DE70
 
我是做的动态生成校验,几年前做的一个测试,好象是8位CRC,贴出来,看是否对你有帮助
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Label1: TLabel;
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//用于定义字节数组类型形参
type
tByteArray=array[1..6]of byte;
var
Form1: TForm1;
implementation

{$R *.DFM}
//CRC校验生成过程;
procedure GenerateCRC(var aPackage:tBytearray;const iLen:integer);
//tBytearray :type
// tByteArray=array[1..6]of byte;
//aPackage:待编码串(含CRC校验字)
//iLen :待编码串长度
var
CRC,SaveByte:Byte;
CRC_DXS:Byte;
i,j:integer;
begin
CRC:=$0;
CRC_DXS:=$07; //生成多项式码串
for i:=1 to 5 do
begin
CRC:=CRC xor aPackage;
for j:=0 to 7 do
begin
SaveByte:=CRC;
CRC:=CRC shl 1;
if ((SaveByte and $80)=$80)then
begin
CRC:=CRC xor CRC_DXS;
end;
end;
end;
aPackage[iLen]:=not CRC;
end;
////CRC校验函数;
function CheckCRC(RevPackage:tBytearray;const iLen:integer):boolean;
//RevPackage:接收码串
//iLen :接收码串长度
var
Ori_CRC:Byte; //接收到的CRC校验码
begin
Ori_CRC:=RevPackage[iLen];
GenerateCRC(RevPackage,6); //调用CRC生成过程,重新生成CRC校验码
if Ori_CRC=RevPackage[iLen] then Result:=True //比对两个校验码
else Result:=False;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
MyPackage:tBytearray;
i:integer;
begin
MyPackage[1]:=strtoint(edit1.text);
MyPackage[2]:=strtoint(edit2.text);
MyPackage[3]:=strtoint(edit3.text);
MyPackage[4]:=strtoint(edit4.text);
MyPackage[5]:=strtoint(edit5.text);
GenerateCRC(MyPackage,6);
label1.caption:=inttostr(MyPackage[6]);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
edit1.text:='';
edit2.text:='';
edit3.text:='';
edit4.text:='';
edit5.text:='';
label1.Caption :='';

end;

end.
 
去查我以前的贴子,这个算法我发过的,seed=$8005
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部