crc16校验问题. (300分)

  • 主题发起人 主题发起人 BlackDragon
  • 开始时间 开始时间
B

BlackDragon

Unregistered / Unconfirmed
GUEST, unregistred user!
var
sd:shortstring;
data:array[0..5] of byte;
........
begin
data[0]:=10;
....
data[5]:=33;
sd:=chr(data[0])+chr(data[1])+chr(data[2])+chr(data[3])+chr(data[4])+chr(data[5]);
....//对sd进行crc16校验,得出校验码
edit1.text:=crclo;//crc16低位
edit2.text:=crchi://crc16高位

这crc16的校验如何做?我做的总出错.请大家帮帮忙,写出crclo和crchi是如何计算出来的.
 
c语言的CRC简单函数如下:

 

unsigned short CRC16(puchMsg, usDataLen)

unsigned char *puchMsg ; /* 要进行CRC校验的消息 */

unsigned short usDataLen ; /* 消息中字节数 */

{

unsigned char uchCRCHi = 0xFF ; /* 高CRC字节初始化 */

unsigned char uchCRCLo = 0xFF ; /* 低CRC 字节初始化 */

unsigned uIndex ; /* CRC循环中的索引 */

while (usDataLen--) /* 传输消息缓冲区 */

{

uIndex = uchCRCHi ^ *puchMsgg++ ; /* 计算CRC */

uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex} ;

uchCRCLo = auchCRCLo[uIndex] ;

}

return (uchCRCHi << 8 | uchCRCLo) ;

}

 

/* CRC 高位字节值表 */

static unsigned char auchCRCHi[] = {

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,

0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,

0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,

0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,

0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,

0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,

0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,

0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,

0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,

0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,

0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,

0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,

0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,

0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,

0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,

0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,

0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,

0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,

0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,

0x80, 0x41, 0x00, 0xC1, 0x81, 0x40

} ;

/* 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, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,

0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,

0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,

0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,

0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,

0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,

0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,

0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,

0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,

0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,

0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,

0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,

0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,

0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,

0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,

0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,

0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,

0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,

0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,

0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,

0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,

0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,

0x43, 0x83, 0x41, 0x81, 0x80, 0x40

} ;
 
function crc16(puchMsg,usDataLen):char;
var uchCRCHi,uchCRCLo :char;
uIndex,i:integer;
e : real;
begin
uchCRCHi:=0xFF;
uchCRCLo:=0xFF;
i:=1;
while (i>usDataLen) do
begin
uIndex:=uchCRCHi^puchMsg; //取开方
uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex];
uchCRCLo = auchCRCHi[uIndex];
end;

result:= (uchCRCHi shl 8) or uchCRCLo

end;
 
crclo := lo(word(StrToInt(sd)));
crchi := hi(word(StrToInt(sd)));
是不是这样.
 
这是用查表法计算 CRC,天真已经给你翻译出来了,你自己建一个上面那样的数组就差不多了
计算crc还有一种用公式计算来实现的,只是没有查表法快,但对于目前CPU的处理速度 已经算不上什么了
你到GOOGLE搜一搜[:D]
 
function crc16(puchMsg,usDataLen):char;
var uchCRCHi,uchCRCLo :char;
uIndex,i:integer;
begin
uchCRCHi:=$FF;
uchCRCLo:=$FF;
i:=1;
for i:=usDataLen downto 1 do
begin
uIndex:=uchCRCHi xor puchMsg;
uchCRCHi := uchCRCLo xor auchCRCHi;
uchCRCLo := auchCRCHi;
end;

result:= (uchCRCHi shl 8) or uchCRCLo

end;
 
for i:=usDataLen downto 1 do
~~~~~~~~~~~~~~~~~~~~~~~~~~~????老大,好象写反了,你
 
我的也不对.再改.
 
如果实在不行的话你就用下面的代码吧:
//calculate CRCR16/CRC32 Checksum, CRC is default $FFFFFFFF,
//after calc you must inverse Result with NOT
function CRC16(CRC: Word; Data: Pointer; DataSize: LongWord): Word;
function CRC32(CRC: LongWord; Data: Pointer; DataSize: LongWord): LongWord;

function CRC16(CRC: Word; Data: Pointer; DataSize: LongWord): Word; assembler;
asm
AND EDX,EDX
JZ @Exit
AND ECX,ECX
JLE @Exit
PUSH EBX
PUSH EDI
XOR EBX,EBX
LEA EDI,CS:[OFFSET @CRC16]
@Start: MOV BL,[EDX]
XOR BL,AL
SHR AX,8
XOR AX,[EDI + EBX * 2]
INC EDX
DEC ECX
JNZ @Start
POP EDI
POP EBX
@Exit: RET
NOP
@CRC16: DW 00000h, 0C0C1h, 0C181h, 00140h, 0C301h, 003C0h, 00280h, 0C241h
DW 0C601h, 006C0h, 00780h, 0C741h, 00500h, 0C5C1h, 0C481h, 00440h
DW 0CC01h, 00CC0h, 00D80h, 0CD41h, 00F00h, 0CFC1h, 0CE81h, 00E40h
DW 00A00h, 0CAC1h, 0CB81h, 00B40h, 0C901h, 009C0h, 00880h, 0C841h
DW 0D801h, 018C0h, 01980h, 0D941h, 01B00h, 0DBC1h, 0DA81h, 01A40h
DW 01E00h, 0DEC1h, 0DF81h, 01F40h, 0DD01h, 01DC0h, 01C80h, 0DC41h
DW 01400h, 0D4C1h, 0D581h, 01540h, 0D701h, 017C0h, 01680h, 0D641h
DW 0D201h, 012C0h, 01380h, 0D341h, 01100h, 0D1C1h, 0D081h, 01040h
DW 0F001h, 030C0h, 03180h, 0F141h, 03300h, 0F3C1h, 0F281h, 03240h
DW 03600h, 0F6C1h, 0F781h, 03740h, 0F501h, 035C0h, 03480h, 0F441h
DW 03C00h, 0FCC1h, 0FD81h, 03D40h, 0FF01h, 03FC0h, 03E80h, 0FE41h
DW 0FA01h, 03AC0h, 03B80h, 0FB41h, 03900h, 0F9C1h, 0F881h, 03840h
DW 02800h, 0E8C1h, 0E981h, 02940h, 0EB01h, 02BC0h, 02A80h, 0EA41h
DW 0EE01h, 02EC0h, 02F80h, 0EF41h, 02D00h, 0EDC1h, 0EC81h, 02C40h
DW 0E401h, 024C0h, 02580h, 0E541h, 02700h, 0E7C1h, 0E681h, 02640h
DW 02200h, 0E2C1h, 0E381h, 02340h, 0E101h, 021C0h, 02080h, 0E041h
DW 0A001h, 060C0h, 06180h, 0A141h, 06300h, 0A3C1h, 0A281h, 06240h
DW 06600h, 0A6C1h, 0A781h, 06740h, 0A501h, 065C0h, 06480h, 0A441h
DW 06C00h, 0ACC1h, 0AD81h, 06D40h, 0AF01h, 06FC0h, 06E80h, 0AE41h
DW 0AA01h, 06AC0h, 06B80h, 0AB41h, 06900h, 0A9C1h, 0A881h, 06840h
DW 07800h, 0B8C1h, 0B981h, 07940h, 0BB01h, 07BC0h, 07A80h, 0BA41h
DW 0BE01h, 07EC0h, 07F80h, 0BF41h, 07D00h, 0BDC1h, 0BC81h, 07C40h
DW 0B401h, 074C0h, 07580h, 0B541h, 07700h, 0B7C1h, 0B681h, 07640h
DW 07200h, 0B2C1h, 0B381h, 07340h, 0B101h, 071C0h, 07080h, 0B041h
DW 05000h, 090C1h, 09181h, 05140h, 09301h, 053C0h, 05280h, 09241h
DW 09601h, 056C0h, 05780h, 09741h, 05500h, 095C1h, 09481h, 05440h
DW 09C01h, 05CC0h, 05D80h, 09D41h, 05F00h, 09FC1h, 09E81h, 05E40h
DW 05A00h, 09AC1h, 09B81h, 05B40h, 09901h, 059C0h, 05880h, 09841h
DW 08801h, 048C0h, 04980h, 08941h, 04B00h, 08BC1h, 08A81h, 04A40h
DW 04E00h, 08EC1h, 08F81h, 04F40h, 08D01h, 04DC0h, 04C80h, 08C41h
DW 04400h, 084C1h, 08581h, 04540h, 08701h, 047C0h, 04680h, 08641h
DW 08201h, 042C0h, 04380h, 08341h, 04100h, 081C1h, 08081h, 04040h
end;

function CRC32(CRC: LongWord; Data: Pointer; DataSize: LongWord): LongWord; assembler;
asm
AND EDX,EDX
JZ @Exit
AND ECX,ECX
JLE @Exit
PUSH EBX
PUSH EDI
XOR EBX,EBX
LEA EDI,CS:[OFFSET @CRC32]
@Start: MOV BL,AL
SHR EAX,8
XOR BL,[EDX]
XOR EAX,[EDI + EBX * 4]
INC EDX
DEC ECX
JNZ @Start
POP EDI
POP EBX
@Exit: RET
DB 0, 0, 0, 0, 0 // Align Table
@CRC32: DD 000000000h, 077073096h, 0EE0E612Ch, 0990951BAh
DD 0076DC419h, 0706AF48Fh, 0E963A535h, 09E6495A3h
DD 00EDB8832h, 079DCB8A4h, 0E0D5E91Eh, 097D2D988h
DD 009B64C2Bh, 07EB17CBDh, 0E7B82D07h, 090BF1D91h
DD 01DB71064h, 06AB020F2h, 0F3B97148h, 084BE41DEh
DD 01ADAD47Dh, 06DDDE4EBh, 0F4D4B551h, 083D385C7h
DD 0136C9856h, 0646BA8C0h, 0FD62F97Ah, 08A65C9ECh
DD 014015C4Fh, 063066CD9h, 0FA0F3D63h, 08D080DF5h
DD 03B6E20C8h, 04C69105Eh, 0D56041E4h, 0A2677172h
DD 03C03E4D1h, 04B04D447h, 0D20D85FDh, 0A50AB56Bh
DD 035B5A8FAh, 042B2986Ch, 0DBBBC9D6h, 0ACBCF940h
DD 032D86CE3h, 045DF5C75h, 0DCD60DCFh, 0ABD13D59h
DD 026D930ACh, 051DE003Ah, 0C8D75180h, 0BFD06116h
DD 021B4F4B5h, 056B3C423h, 0CFBA9599h, 0B8BDA50Fh
DD 02802B89Eh, 05F058808h, 0C60CD9B2h, 0B10BE924h
DD 02F6F7C87h, 058684C11h, 0C1611DABh, 0B6662D3Dh
DD 076DC4190h, 001DB7106h, 098D220BCh, 0EFD5102Ah
DD 071B18589h, 006B6B51Fh, 09FBFE4A5h, 0E8B8D433h
DD 07807C9A2h, 00F00F934h, 09609A88Eh, 0E10E9818h
DD 07F6A0DBBh, 0086D3D2Dh, 091646C97h, 0E6635C01h
DD 06B6B51F4h, 01C6C6162h, 0856530D8h, 0F262004Eh
DD 06C0695EDh, 01B01A57Bh, 08208F4C1h, 0F50FC457h
DD 065B0D9C6h, 012B7E950h, 08BBEB8EAh, 0FCB9887Ch
DD 062DD1DDFh, 015DA2D49h, 08CD37CF3h, 0FBD44C65h
DD 04DB26158h, 03AB551CEh, 0A3BC0074h, 0D4BB30E2h
DD 04ADFA541h, 03DD895D7h, 0A4D1C46Dh, 0D3D6F4FBh
DD 04369E96Ah, 0346ED9FCh, 0AD678846h, 0DA60B8D0h
DD 044042D73h, 033031DE5h, 0AA0A4C5Fh, 0DD0D7CC9h
DD 05005713Ch, 0270241AAh, 0BE0B1010h, 0C90C2086h
DD 05768B525h, 0206F85B3h, 0B966D409h, 0CE61E49Fh
DD 05EDEF90Eh, 029D9C998h, 0B0D09822h, 0C7D7A8B4h
DD 059B33D17h, 02EB40D81h, 0B7BD5C3Bh, 0C0BA6CADh
DD 0EDB88320h, 09ABFB3B6h, 003B6E20Ch, 074B1D29Ah
DD 0EAD54739h, 09DD277AFh, 004DB2615h, 073DC1683h
DD 0E3630B12h, 094643B84h, 00D6D6A3Eh, 07A6A5AA8h
DD 0E40ECF0Bh, 09309FF9Dh, 00A00AE27h, 07D079EB1h
DD 0F00F9344h, 08708A3D2h, 01E01F268h, 06906C2FEh
DD 0F762575Dh, 0806567CBh, 0196C3671h, 06E6B06E7h
DD 0FED41B76h, 089D32BE0h, 010DA7A5Ah, 067DD4ACCh
DD 0F9B9DF6Fh, 08EBEEFF9h, 017B7BE43h, 060B08ED5h
DD 0D6D6A3E8h, 0A1D1937Eh, 038D8C2C4h, 04FDFF252h
DD 0D1BB67F1h, 0A6BC5767h, 03FB506DDh, 048B2364Bh
DD 0D80D2BDAh, 0AF0A1B4Ch, 036034AF6h, 041047A60h
DD 0DF60EFC3h, 0A867DF55h, 0316E8EEFh, 04669BE79h
DD 0CB61B38Ch, 0BC66831Ah, 0256FD2A0h, 05268E236h
DD 0CC0C7795h, 0BB0B4703h, 0220216B9h, 05505262Fh
DD 0C5BA3BBEh, 0B2BD0B28h, 02BB45A92h, 05CB36A04h
DD 0C2D7FFA7h, 0B5D0CF31h, 02CD99E8Bh, 05BDEAE1Dh
DD 09B64C2B0h, 0EC63F226h, 0756AA39Ch, 0026D930Ah
DD 09C0906A9h, 0EB0E363Fh, 072076785h, 005005713h
DD 095BF4A82h, 0E2B87A14h, 07BB12BAEh, 00CB61B38h
DD 092D28E9Bh, 0E5D5BE0Dh, 07CDCEFB7h, 00BDBDF21h
DD 086D3D2D4h, 0F1D4E242h, 068DDB3F8h, 01FDA836Eh
DD 081BE16CDh, 0F6B9265Bh, 06FB077E1h, 018B74777h
DD 088085AE6h, 0FF0F6A70h, 066063BCAh, 011010B5Ch
DD 08F659EFFh, 0F862AE69h, 0616BFFD3h, 0166CCF45h
DD 0A00AE278h, 0D70DD2EEh, 04E048354h, 03903B3C2h
DD 0A7672661h, 0D06016F7h, 04969474Dh, 03E6E77DBh
DD 0AED16A4Ah, 0D9D65ADCh, 040DF0B66h, 037D83BF0h
DD 0A9BCAE53h, 0DEBB9EC5h, 047B2CF7Fh, 030B5FFE9h
DD 0BDBDF21Ch, 0CABAC28Ah, 053B39330h, 024B4A3A6h
DD 0BAD03605h, 0CDD70693h, 054DE5729h, 023D967BFh
DD 0B3667A2Eh, 0C4614AB8h, 05D681B02h, 02A6F2B94h
DD 0B40BBE37h, 0C30C8EA1h, 05A05DF1Bh, 02D02EF8Dh
DD 074726F50h, 0736E6F69h, 0706F4320h, 067697279h
DD 028207468h, 031202963h, 020393939h, 048207962h
DD 06E656761h, 064655220h, 06E616D64h, 06FBBA36Eh
end;

调用:
CRC := not CRC16($FFFF, M.Memory, M.Size);
 
Command message

ADR---------------01H
CMD---------------03H
Starting data--------21H
address-----------02H
Number of------------00H
data--------------02H
CRC Low------------6FH
CRC Hig------------F7H



ADR---------------01H
CMD---------------06H
Starting data--------01H
address-----------00H
Number of------------17H
data--------------70H
CRC Low------------86H
CRC Hig------------22H
 
这是vb的.

Private Function CRC16(data() As Byte) As String

   Dim CRC16Hi As Byte

   Dim CRC16Lo As Byte

   CRC16Hi = &HFF

   CRC16Lo = &HFF

   Dim i As Integer

   Dim iIndex As Long

   For i = 0 To UBound(data)

    iIndex = CRC16Lo Xor data(i)

    CRC16Lo = CRC16Hi Xor GetCRCLo(iIndex)    ’低位处理

    CRC16Hi = GetCRCHi(iIndex)          ’高位处理

   Next i

   Dim ReturnData(1) As Byte

   ReturnData(0) = CRC16Hi    ’CRC高位

   ReturnData(1) = CRC16Lo    ’CRC低位

   CRC16 = ReturnData

  End Function


  ’CRC低位字节值表

  Function GetCRCLo(Ind As Long) As Byte

   GetCRCLo = Choose(Ind + 1, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, _

&H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40)

  End Function


  ’CRC高位字节值表

  Function GetCRCHi(Ind As Long) As Byte

   GetCRCHi = Choose(Ind + 1, &H0, &HC0, &HC1, &H1, &HC3, &H3, &H2, &HC2, &HC6, &H6, &H7, &HC7, &H5, &HC5, &HC4, &H4, &HCC, &HC, &HD, &HCD, &HF, &HCF, &HCE, &HE, &HA, &HCA, &HCB, &HB, &HC9, &H9, &H8, &HC8, &HD8, &H18, &H19, &HD9, &H1B, &HDB, &HDA, &H1A, &H1E, &HDE, &HDF, &H1F, &HDD, &H1D, &H1C, &HDC, &H14, &HD4, &HD5, &H15, &HD7, &H17, &H16, &HD6, &HD2, &H12, &H13, &HD3, &H11, &HD1, &HD0, &H10, &HF0, &H30, &H31, &HF1, &H33, &HF3, &HF2, &H32, &H36, &HF6, &HF7, &H37, &HF5, &H35, &H34, &HF4, &H3C, &HFC, &HFD, &H3D, &HFF, &H3F, &H3E, &HFE, &HFA, &H3A, &H3B, &HFB, &H39, &HF9, &HF8, &H38, &H28, &HE8, &HE9, &H29, &HEB, &H2B, &H2A, &HEA, &HEE, &H2E, &H2F, &HEF, &H2D, &HED, &HEC, &H2C, &HE4, &H24, &H25, &HE5, &H27, &HE7, &HE6, &H26, &H22, &HE2, &HE3, &H23, &HE1, &H21, &H20, &HE0, &HA0, &H60, _

&H61, &HA1, &H63, &HA3, &HA2, &H62, &H66, &HA6, &HA7, &H67, &HA5, &H65, &H64, &HA4, &H6C, &HAC, &HAD, &H6D, &HAF, &H6F, &H6E, &HAE, &HAA, &H6A, &H6B, &HAB, &H69, &HA9, &HA8, &H68, &H78, &HB8, &HB9, &H79, &HBB, &H7B, &H7A, &HBA, &HBE, &H7E, &H7F, &HBF, &H7D, &HBD, &HBC, &H7C, &HB4, &H74, &H75, &HB5, &H77, &HB7, &HB6, &H76, &H72, &HB2, &HB3, &H73, &HB1, &H71, &H70, &HB0, &H50, &H90, &H91, &H51, &H93, &H53, &H52, &H92, &H96, &H56, &H57, &H97, &H55, &H95, &H94, &H54, &H9C, &H5C, &H5D, &H9D, &H5F, &H9F, &H9E, &H5E, &H5A, &H9A, &H9B, &H5B, &H99, &H59, &H58, &H98, &H88, &H48, &H49, &H89, &H4B, &H8B, &H8A, &H4A, &H4E, &H8E, &H8F, &H4F, &H8D, &H4D, &H4C, &H8C, &H44, &H84, &H85, &H45, &H87, &H47, &H46, &H86, &H82, &H42, &H43, &H83, &H41, &H81, &H80, &H40)

  End Function
 
crc16有两种,这只是其中的一种.

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Button1: TButton;
Button2: TButton;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

const
Crc16Table: array[0..$FF] of Word =
(
$0000, $C0C1, $C181, $0140, $C301, $03C0, $0280, $C241,
$C601, $06C0, $0780, $C741, $0500, $C5C1, $C481, $0440,
$CC01, $0CC0, $0D80, $CD41, $0F00, $CFC1, $CE81, $0E40,
$0A00, $CAC1, $CB81, $0B40, $C901, $09C0, $0880, $C841,
$D801, $18C0, $1980, $D941, $1B00, $DBC1, $DA81, $1A40,
$1E00, $DEC1, $DF81, $1F40, $DD01, $1DC0, $1C80, $DC41,
$1400, $D4C1, $D581, $1540, $D701, $17C0, $1680, $D641,
$D201, $12C0, $1380, $D341, $1100, $D1C1, $D081, $1040,
$F001, $30C0, $3180, $F141, $3300, $F3C1, $F281, $3240,
$3600, $F6C1, $F781, $3740, $F501, $35C0, $3480, $F441,
$3C00, $FCC1, $FD81, $3D40, $FF01, $3FC0, $3E80, $FE41,
$FA01, $3AC0, $3B80, $FB41, $3900, $F9C1, $F881, $3840,
$2800, $E8C1, $E981, $2940, $EB01, $2BC0, $2A80, $EA41,
$EE01, $2EC0, $2F80, $EF41, $2D00, $EDC1, $EC81, $2C40,
$E401, $24C0, $2580, $E541, $2700, $E7C1, $E681, $2640,
$2200, $E2C1, $E381, $2340, $E101, $21C0, $2080, $E041,
$A001, $60C0, $6180, $A141, $6300, $A3C1, $A281, $6240,
$6600, $A6C1, $A781, $6740, $A501, $65C0, $6480, $A441,
$6C00, $ACC1, $AD81, $6D40, $AF01, $6FC0, $6E80, $AE41,
$AA01, $6AC0, $6B80, $AB41, $6900, $A9C1, $A881, $6840,
$7800, $B8C1, $B981, $7940, $BB01, $7BC0, $7A80, $BA41,
$BE01, $7EC0, $7F80, $BF41, $7D00, $BDC1, $BC81, $7C40,
$B401, $74C0, $7580, $B541, $7700, $B7C1, $B681, $7640,
$7200, $B2C1, $B381, $7340, $B101, $71C0, $7080, $B041,
$5000, $90C1, $9181, $5140, $9301, $53C0, $5280, $9241,
$9601, $56C0, $5780, $9741, $5500, $95C1, $9481, $5440,
$9C01, $5CC0, $5D80, $9D41, $5F00, $9FC1, $9E81, $5E40,
$5A00, $9AC1, $9B81, $5B40, $9901, $59C0, $5880, $9841,
$8801, $48C0, $4980, $8941, $4B00, $8BC1, $8A81, $4A40,
$4E00, $8EC1, $8F81, $4F40, $8D01, $4DC0, $4C80, $8C41,
$4400, $84C1, $8581, $4540, $8701, $47C0, $4680, $8641,
$8201, $42C0, $4380, $8341, $4100, $81C1, $8081, $4040
);

var
Form1: TForm1;

implementation

{$R *.dfm}

const
Buffer_Size = 8192;

function Crc16_std(InitCrc: Word; Buffer: PChar; Length: Integer): Word;
var
Crc: Word;
begin
//Crc := InitCrc;
while Length > 0 do
begin
Crc := (Crc shr 8) xor CRC16Table[(Crc xor (Byte(Buffer^))) and $00FF];
Inc(Buffer);
Dec(Length);
end;
Result := Crc;
end;

function CalcFileCrc(FileName: string): Word;
var
Stream: TFileStream;
BlockSize: Integer;
Buffer: array[0..Buffer_Size - 1] of Byte;
begin
Result := $FFFF;
try
Stream := TFileStream.Create(Filename, fmOpenRead or fmShareDenyWrite);
while (Stream.Position <> Stream.Size) do
begin
BlockSize := Stream.Read(Buffer, Sizeof(Buffer));
Crc16_Std(Result, @Buffer, BlockSize);
end;
finally
stream.Free;
end;
Result := not Result;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
wResult: Word;
begin
wResult := $FFFF;
//Crc16(PChar(Edit1.Text), Length(Edit1.Text), wResult);
//Label1.Caption := IntToHex(wResult, 2);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Label2.Caption := IntToHex(Crc16_Std($FFFF, PChar(Edit1.Text), Length(Edit1.Text)), 2);
end;

end.

 
我来也!
//循环冗余校验(16Bit),返回Hex
function CRC16(const AData: array of Byte): String;
var
I, Flag: Integer;
CRC16High, CRC16Low, SaveHigh, SaveLow: Byte;
begin
CRC16High:= $FF;
CRC16Low:= $FF;
for I:= 0 to High(AData) do
begin
CRC16Low:= CRC16Low xor AData;
for Flag:= 0 to 7 do
begin
SaveHigh:= CRC16High;
SaveLow:= CRC16Low;
CRC16High:= CRC16High shr 1;
CRC16Low:= CRC16Low shr 1;
if (SaveHigh and $1) = $1 then
CRC16Low:= CRC16Low or $80;
if (SaveLow and $1) = $1 then
begin
CRC16High:= CRC16High xor CH;
CRC16Low:= CRC16Low xor CL;
end;
end;
end;
Result:= IntToHex(CRC16High, 2) + IntToHex(CRC16Low, 2);
end;
 
这个问题比较麻烦哦,需要的话留下email,我发个例子给你。
或联系:mumuhai@sohu.com
 
LoByte(Value: Word) 返回低位
HiByte(Value: Word) 返回高位
 
To Zane:
你这个不对啊,如果计算data的校验值,0207返回校验值为4112.
 
To Zane:
你这个不对啊,如果计算data的校验值,0207返回校验值应为4112.
To moodboy:
zhaohai9@163.net
BTW,你的例子我希望是你的,不要深度历险里的东西.

衍生多项式为:G(X)=X16+X15+X2+1

 
这是我找到的CRC校验函数

unit Crc32;

interface

function GetCrc32(s: string; Seed: LongInt): string;
function GetCrc32ForFile(AFileName: string; Seed: LongInt): string;

implementation

uses Windows, SysUtils;

const CrcTbl: array [0..255] of DWORD =
( $00000000, $77073096, $ee0e612c, $990951ba,
$076dc419, $706af48f, $e963a535, $9e6495a3,
$0edb8832, $79dcb8a4, $e0d5e91e, $97d2d988,
$09b64c2b, $7eb17cbd, $e7b82d07, $90bf1d91,

$1db71064, $6ab020f2, $f3b97148, $84be41de,
$1adad47d, $6ddde4eb, $f4d4b551, $83d385c7,
$136c9856, $646ba8c0, $fd62f97a, $8a65c9ec,
$14015c4f, $63066cd9, $fa0f3d63, $8d080df5,

$3b6e20c8, $4c69105e, $d56041e4, $a2677172,
$3c03e4d1, $4b04d447, $d20d85fd, $a50ab56b,
$35b5a8fa, $42b2986c, $dbbbc9d6, $acbcf940,
$32d86ce3, $45df5c75, $dcd60dcf, $abd13d59,

$26d930ac, $51de003a, $c8d75180, $bfd06116,
$21b4f4b5, $56b3c423, $cfba9599, $b8bda50f,
$2802b89e, $5f058808, $c60cd9b2, $b10be924,
$2f6f7c87, $58684c11, $c1611dab, $b6662d3d,

$76dc4190, $01db7106, $98d220bc, $efd5102a,
$71b18589, $06b6b51f, $9fbfe4a5, $e8b8d433,
$7807c9a2, $0f00f934, $9609a88e, $e10e9818,
$7f6a0dbb, $086d3d2d, $91646c97, $e6635c01,

$6b6b51f4, $1c6c6162, $856530d8, $f262004e,
$6c0695ed, $1b01a57b, $8208f4c1, $f50fc457,
$65b0d9c6, $12b7e950, $8bbeb8ea, $fcb9887c,
$62dd1ddf, $15da2d49, $8cd37cf3, $fbd44c65,

$4db26158, $3ab551ce, $a3bc0074, $d4bb30e2,
$4adfa541, $3dd895d7, $a4d1c46d, $d3d6f4fb,
$4369e96a, $346ed9fc, $ad678846, $da60b8d0,
$44042d73, $33031de5, $aa0a4c5f, $dd0d7cc9,

$5005713c, $270241aa, $be0b1010, $c90c2086,
$5768b525, $206f85b3, $b966d409, $ce61e49f,
$5edef90e, $29d9c998, $b0d09822, $c7d7a8b4,
$59b33d17, $2eb40d81, $b7bd5c3b, $c0ba6cad,

$edb88320, $9abfb3b6, $03b6e20c, $74b1d29a,
$ead54739, $9dd277af, $04db2615, $73dc1683,
$e3630b12, $94643b84, $0d6d6a3e, $7a6a5aa8,
$e40ecf0b, $9309ff9d, $0a00ae27, $7d079eb1,

$f00f9344, $8708a3d2, $1e01f268, $6906c2fe,
$f762575d, $806567cb, $196c3671, $6e6b06e7,
$fed41b76, $89d32be0, $10da7a5a, $67dd4acc,
$f9b9df6f, $8ebeeff9, $17b7be43, $60b08ed5,

$d6d6a3e8, $a1d1937e, $38d8c2c4, $4fdff252,
$d1bb67f1, $a6bc5767, $3fb506dd, $48b2364b,
$d80d2bda, $af0a1b4c, $36034af6, $41047a60,
$df60efc3, $a867df55, $316e8eef, $4669be79,

$cb61b38c, $bc66831a, $256fd2a0, $5268e236,
$cc0c7795, $bb0b4703, $220216b9, $5505262f,
$c5ba3bbe, $b2bd0b28, $2bb45a92, $5cb36a04,
$c2d7ffa7, $b5d0cf31, $2cd99e8b, $5bdeae1d,

$9b64c2b0, $ec63f226, $756aa39c, $026d930a,
$9c0906a9, $eb0e363f, $72076785, $05005713,
$95bf4a82, $e2b87a14, $7bb12bae, $0cb61b38,
$92d28e9b, $e5d5be0d, $7cdcefb7, $0bdbdf21,

$86d3d2d4, $f1d4e242, $68ddb3f8, $1fda836e,
$81be16cd, $f6b9265b, $6fb077e1, $18b74777,
$88085ae6, $ff0f6a70, $66063bca, $11010b5c,
$8f659eff, $f862ae69, $616bffd3, $166ccf45,

$a00ae278, $d70dd2ee, $4e048354, $3903b3c2,
$a7672661, $d06016f7, $4969474d, $3e6e77db,
$aed16a4a, $d9d65adc, $40df0b66, $37d83bf0,
$a9bcae53, $debb9ec5, $47b2cf7f, $30b5ffe9,

$bdbdf21c, $cabac28a, $53b39330, $24b4a3a6,
$bad03605, $cdd70693, $54de5729, $23d967bf,
$b3667a2e, $c4614ab8, $5d681b02, $2a6f2b94,
$b40bbe37, $c30c8ea1, $5a05df1b, $2d02ef8d);

function GetCrc32(s: string; Seed: LongInt): string;
var Count: Integer;
CrcVal: LongInt;
begin
CrcVal := Seed;
for Count := 1 to Length(s) do
CrcVal := CrcTbl[Byte(CrcVal xor DWORD(Ord(s[Count])))] xor ((CrcVal shr 8) and $00FFFFFF);
Result := IntToHex(not(CrcVal), 8);
end;

I think this will do what you want:

unit Crc32;

interface

function GetCrc32(s: string; Seed: LongInt): string;
function GetCrc32ForFile(AFileName: string; Seed: LongInt): string;
function GetCrc32ForStream(AStream : TMemoryStream; Seed: Longint): String;

implementation

uses Windows, SysUtils;

const CrcTbl: array [0..255] of DWORD =
( $00000000, $77073096, $ee0e612c, $990951ba,
$076dc419, $706af48f, $e963a535, $9e6495a3,
$0edb8832, $79dcb8a4, $e0d5e91e, $97d2d988,
$09b64c2b, $7eb17cbd, $e7b82d07, $90bf1d91,

$1db71064, $6ab020f2, $f3b97148, $84be41de,
$1adad47d, $6ddde4eb, $f4d4b551, $83d385c7,
$136c9856, $646ba8c0, $fd62f97a, $8a65c9ec,
$14015c4f, $63066cd9, $fa0f3d63, $8d080df5,

$3b6e20c8, $4c69105e, $d56041e4, $a2677172,
$3c03e4d1, $4b04d447, $d20d85fd, $a50ab56b,
$35b5a8fa, $42b2986c, $dbbbc9d6, $acbcf940,
$32d86ce3, $45df5c75, $dcd60dcf, $abd13d59,

$26d930ac, $51de003a, $c8d75180, $bfd06116,
$21b4f4b5, $56b3c423, $cfba9599, $b8bda50f,
$2802b89e, $5f058808, $c60cd9b2, $b10be924,
$2f6f7c87, $58684c11, $c1611dab, $b6662d3d,

$76dc4190, $01db7106, $98d220bc, $efd5102a,
$71b18589, $06b6b51f, $9fbfe4a5, $e8b8d433,
$7807c9a2, $0f00f934, $9609a88e, $e10e9818,
$7f6a0dbb, $086d3d2d, $91646c97, $e6635c01,

$6b6b51f4, $1c6c6162, $856530d8, $f262004e,
$6c0695ed, $1b01a57b, $8208f4c1, $f50fc457,
$65b0d9c6, $12b7e950, $8bbeb8ea, $fcb9887c,
$62dd1ddf, $15da2d49, $8cd37cf3, $fbd44c65,

$4db26158, $3ab551ce, $a3bc0074, $d4bb30e2,
$4adfa541, $3dd895d7, $a4d1c46d, $d3d6f4fb,
$4369e96a, $346ed9fc, $ad678846, $da60b8d0,
$44042d73, $33031de5, $aa0a4c5f, $dd0d7cc9,

$5005713c, $270241aa, $be0b1010, $c90c2086,
$5768b525, $206f85b3, $b966d409, $ce61e49f,
$5edef90e, $29d9c998, $b0d09822, $c7d7a8b4,
$59b33d17, $2eb40d81, $b7bd5c3b, $c0ba6cad,

$edb88320, $9abfb3b6, $03b6e20c, $74b1d29a,
$ead54739, $9dd277af, $04db2615, $73dc1683,
$e3630b12, $94643b84, $0d6d6a3e, $7a6a5aa8,
$e40ecf0b, $9309ff9d, $0a00ae27, $7d079eb1,

$f00f9344, $8708a3d2, $1e01f268, $6906c2fe,
$f762575d, $806567cb, $196c3671, $6e6b06e7,
$fed41b76, $89d32be0, $10da7a5a, $67dd4acc,
$f9b9df6f, $8ebeeff9, $17b7be43, $60b08ed5,

$d6d6a3e8, $a1d1937e, $38d8c2c4, $4fdff252,
$d1bb67f1, $a6bc5767, $3fb506dd, $48b2364b,
$d80d2bda, $af0a1b4c, $36034af6, $41047a60,
$df60efc3, $a867df55, $316e8eef, $4669be79,

$cb61b38c, $bc66831a, $256fd2a0, $5268e236,
$cc0c7795, $bb0b4703, $220216b9, $5505262f,
$c5ba3bbe, $b2bd0b28, $2bb45a92, $5cb36a04,
$c2d7ffa7, $b5d0cf31, $2cd99e8b, $5bdeae1d,

$9b64c2b0, $ec63f226, $756aa39c, $026d930a,
$9c0906a9, $eb0e363f, $72076785, $05005713,
$95bf4a82, $e2b87a14, $7bb12bae, $0cb61b38,
$92d28e9b, $e5d5be0d, $7cdcefb7, $0bdbdf21,

$86d3d2d4, $f1d4e242, $68ddb3f8, $1fda836e,
$81be16cd, $f6b9265b, $6fb077e1, $18b74777,
$88085ae6, $ff0f6a70, $66063bca, $11010b5c,
$8f659eff, $f862ae69, $616bffd3, $166ccf45,

$a00ae278, $d70dd2ee, $4e048354, $3903b3c2,
$a7672661, $d06016f7, $4969474d, $3e6e77db,
$aed16a4a, $d9d65adc, $40df0b66, $37d83bf0,
$a9bcae53, $debb9ec5, $47b2cf7f, $30b5ffe9,

$bdbdf21c, $cabac28a, $53b39330, $24b4a3a6,
$bad03605, $cdd70693, $54de5729, $23d967bf,
$b3667a2e, $c4614ab8, $5d681b02, $2a6f2b94,
$b40bbe37, $c30c8ea1, $5a05df1b, $2d02ef8d);

function GetCrc32(s: string; Seed: LongInt): string;
var Count: Integer;
CrcVal: LongInt;
begin
CrcVal := Seed;
for Count := 1 to Length(s) do
CrcVal := CrcTbl[Byte(CrcVal xor DWORD(Ord(s[Count])))] xor ((CrcVal shr 8) and $00FFFFFF);
Result := IntToHex(not(CrcVal), 8);
end;

function GetCrc32ForFile(AFileName: string; Seed: LongInt): string;

begin
Result := '';
CRCVal := Seed;
hFile := FileOpen(AFileName, 0);
if hFile = -1 then
exit;
try
repeat
BytesRead := FileRead(hFile, Buf, SizeOf(Buf));
if BytesRead > 0 then
for Count := 0 to BytesRead - 1 do
CRCVal := CrcTbl[Byte(CRCVal xor LongInt(Buf[Count]))] xor ((CRCVal shr 8) and $00FFFFFF);
until BytesRead = 0;
Result := IntToHex(not(CRCVal), 8);
finally
FileClose(hFile);
end;
end;

function GetCrc32ForStream(AStream : TMemoryStream; Seed: Longint): String;
var
Buf : Array[1..1000] of char;
BufLen : Integer;
Count: Word;
CRCVal: Longint;
begin
Result := '';
CRCVal := Seed;

repeat
BufLen := AStream.Read(Buf, 1000);
if BufLen > 0 then
for Count := 0 to BufLen - 1 do
CRCVal := CrcTbl[Byte(CRCVal xor LongInt(Buf[Count]))] xor ((CRCVal shr 8) and $00FFFFFF);
until BufLen < 1000;

Result := IntToHex(not(CRCVal), 8);
end;

end.

Cheers,

Raymond.




---------------------------------------------------


Oops, Ignore that - a bit of a cut 'n paste frenzy there. This is what I meant to say:



I think this will do what you want:

unit Crc32;

interface

function GetCrc32(s: string; Seed: LongInt): string;
function GetCrc32ForFile(AFileName: string; Seed: LongInt): string;
function GetCrc32ForStream(AStream : TMemoryStream; Seed: Longint): String;

implementation

uses Windows, SysUtils;

const CrcTbl: array [0..255] of DWORD =
( $00000000, $77073096, $ee0e612c, $990951ba,
$076dc419, $706af48f, $e963a535, $9e6495a3,
$0edb8832, $79dcb8a4, $e0d5e91e, $97d2d988,
$09b64c2b, $7eb17cbd, $e7b82d07, $90bf1d91,

$1db71064, $6ab020f2, $f3b97148, $84be41de,
$1adad47d, $6ddde4eb, $f4d4b551, $83d385c7,
$136c9856, $646ba8c0, $fd62f97a, $8a65c9ec,
$14015c4f, $63066cd9, $fa0f3d63, $8d080df5,

$3b6e20c8, $4c69105e, $d56041e4, $a2677172,
$3c03e4d1, $4b04d447, $d20d85fd, $a50ab56b,
$35b5a8fa, $42b2986c, $dbbbc9d6, $acbcf940,
$32d86ce3, $45df5c75, $dcd60dcf, $abd13d59,

$26d930ac, $51de003a, $c8d75180, $bfd06116,
$21b4f4b5, $56b3c423, $cfba9599, $b8bda50f,
$2802b89e, $5f058808, $c60cd9b2, $b10be924,
$2f6f7c87, $58684c11, $c1611dab, $b6662d3d,

$76dc4190, $01db7106, $98d220bc, $efd5102a,
$71b18589, $06b6b51f, $9fbfe4a5, $e8b8d433,
$7807c9a2, $0f00f934, $9609a88e, $e10e9818,
$7f6a0dbb, $086d3d2d, $91646c97, $e6635c01,

$6b6b51f4, $1c6c6162, $856530d8, $f262004e,
$6c0695ed, $1b01a57b, $8208f4c1, $f50fc457,
$65b0d9c6, $12b7e950, $8bbeb8ea, $fcb9887c,
$62dd1ddf, $15da2d49, $8cd37cf3, $fbd44c65,

$4db26158, $3ab551ce, $a3bc0074, $d4bb30e2,
$4adfa541, $3dd895d7, $a4d1c46d, $d3d6f4fb,
$4369e96a, $346ed9fc, $ad678846, $da60b8d0,
$44042d73, $33031de5, $aa0a4c5f, $dd0d7cc9,

$5005713c, $270241aa, $be0b1010, $c90c2086,
$5768b525, $206f85b3, $b966d409, $ce61e49f,
$5edef90e, $29d9c998, $b0d09822, $c7d7a8b4,
$59b33d17, $2eb40d81, $b7bd5c3b, $c0ba6cad,

$edb88320, $9abfb3b6, $03b6e20c, $74b1d29a,
$ead54739, $9dd277af, $04db2615, $73dc1683,
$e3630b12, $94643b84, $0d6d6a3e, $7a6a5aa8,
$e40ecf0b, $9309ff9d, $0a00ae27, $7d079eb1,

$f00f9344, $8708a3d2, $1e01f268, $6906c2fe,
$f762575d, $806567cb, $196c3671, $6e6b06e7,
$fed41b76, $89d32be0, $10da7a5a, $67dd4acc,
$f9b9df6f, $8ebeeff9, $17b7be43, $60b08ed5,

$d6d6a3e8, $a1d1937e, $38d8c2c4, $4fdff252,
$d1bb67f1, $a6bc5767, $3fb506dd, $48b2364b,
$d80d2bda, $af0a1b4c, $36034af6, $41047a60,
$df60efc3, $a867df55, $316e8eef, $4669be79,

$cb61b38c, $bc66831a, $256fd2a0, $5268e236,
$cc0c7795, $bb0b4703, $220216b9, $5505262f,
$c5ba3bbe, $b2bd0b28, $2bb45a92, $5cb36a04,
$c2d7ffa7, $b5d0cf31, $2cd99e8b, $5bdeae1d,

$9b64c2b0, $ec63f226, $756aa39c, $026d930a,
$9c0906a9, $eb0e363f, $72076785, $05005713,
$95bf4a82, $e2b87a14, $7bb12bae, $0cb61b38,
$92d28e9b, $e5d5be0d, $7cdcefb7, $0bdbdf21,

$86d3d2d4, $f1d4e242, $68ddb3f8, $1fda836e,
$81be16cd, $f6b9265b, $6fb077e1, $18b74777,
$88085ae6, $ff0f6a70, $66063bca, $11010b5c,
$8f659eff, $f862ae69, $616bffd3, $166ccf45,

$a00ae278, $d70dd2ee, $4e048354, $3903b3c2,
$a7672661, $d06016f7, $4969474d, $3e6e77db,
$aed16a4a, $d9d65adc, $40df0b66, $37d83bf0,
$a9bcae53, $debb9ec5, $47b2cf7f, $30b5ffe9,

$bdbdf21c, $cabac28a, $53b39330, $24b4a3a6,
$bad03605, $cdd70693, $54de5729, $23d967bf,
$b3667a2e, $c4614ab8, $5d681b02, $2a6f2b94,
$b40bbe37, $c30c8ea1, $5a05df1b, $2d02ef8d);

function GetCrc32(s: string; Seed: LongInt): string;
var Count: Integer;
CrcVal: LongInt;
begin
CrcVal := Seed;
for Count := 1 to Length(s) do
CrcVal := CrcTbl[Byte(CrcVal xor DWORD(Ord(s[Count])))] xor ((CrcVal shr 8) and $00FFFFFF);
Result := IntToHex(not(CrcVal), 8);
end;

function GetCrc32ForFile(AFileName: string; Seed: LongInt): string;

begin
Result := '';
CRCVal := Seed;
hFile := FileOpen(AFileName, 0);
if hFile = -1 then
exit;
try
repeat
BytesRead := FileRead(hFile, Buf, SizeOf(Buf));
if BytesRead > 0 then
for Count := 0 to BytesRead - 1 do
CRCVal := CrcTbl[Byte(CRCVal xor LongInt(Buf[Count]))] xor ((CRCVal shr 8) and $00FFFFFF);
until BytesRead = 0;
Result := IntToHex(not(CRCVal), 8);
finally
FileClose(hFile);
end;
end;

function GetCrc32ForStream(AStream : TMemoryStream; Seed: Longint): String;
var
Buf : Array[1..1000] of char;
BufLen : Integer;
Count: Word;
CRCVal: Longint;
begin
Result := '';
CRCVal := Seed;

repeat
BufLen := AStream.Read(Buf, 1000);
if BufLen > 0 then
for Count := 0 to BufLen - 1 do
CRCVal := CrcTbl[Byte(CRCVal xor LongInt(Buf[Count]))] xor ((CRCVal shr 8) and $00FFFFFF);
until BufLen < 1000;

Result := IntToHex(not(CRCVal), 8);
end;

end.

Cheers,

Raymond.

-------------------------------------------------

Sorry LostOne. A combination of cut 'n paste goofs and missing a unit in the uses...

unit Crc32;

interface

uses
Classes;

function GetCrc32(s: string; Seed: LongInt): string;
function GetCrc32ForFile(AFileName: string; Seed: LongInt): string;
function GetCrc32ForStream(AStream : TMemoryStream; Seed: Longint): String;

implementation

uses Windows, SysUtils;

const CrcTbl: array [0..255] of DWORD =
( $00000000, $77073096, $ee0e612c, $990951ba,
$076dc419, $706af48f, $e963a535, $9e6495a3,
$0edb8832, $79dcb8a4, $e0d5e91e, $97d2d988,
$09b64c2b, $7eb17cbd, $e7b82d07, $90bf1d91,

$1db71064, $6ab020f2, $f3b97148, $84be41de,
$1adad47d, $6ddde4eb, $f4d4b551, $83d385c7,
$136c9856, $646ba8c0, $fd62f97a, $8a65c9ec,
$14015c4f, $63066cd9, $fa0f3d63, $8d080df5,

$3b6e20c8, $4c69105e, $d56041e4, $a2677172,
$3c03e4d1, $4b04d447, $d20d85fd, $a50ab56b,
$35b5a8fa, $42b2986c, $dbbbc9d6, $acbcf940,
$32d86ce3, $45df5c75, $dcd60dcf, $abd13d59,

$26d930ac, $51de003a, $c8d75180, $bfd06116,
$21b4f4b5, $56b3c423, $cfba9599, $b8bda50f,
$2802b89e, $5f058808, $c60cd9b2, $b10be924,
$2f6f7c87, $58684c11, $c1611dab, $b6662d3d,

$76dc4190, $01db7106, $98d220bc, $efd5102a,
$71b18589, $06b6b51f, $9fbfe4a5, $e8b8d433,
$7807c9a2, $0f00f934, $9609a88e, $e10e9818,
$7f6a0dbb, $086d3d2d, $91646c97, $e6635c01,

$6b6b51f4, $1c6c6162, $856530d8, $f262004e,
$6c0695ed, $1b01a57b, $8208f4c1, $f50fc457,
$65b0d9c6, $12b7e950, $8bbeb8ea, $fcb9887c,
$62dd1ddf, $15da2d49, $8cd37cf3, $fbd44c65,

$4db26158, $3ab551ce, $a3bc0074, $d4bb30e2,
$4adfa541, $3dd895d7, $a4d1c46d, $d3d6f4fb,
$4369e96a, $346ed9fc, $ad678846, $da60b8d0,
$44042d73, $33031de5, $aa0a4c5f, $dd0d7cc9,

$5005713c, $270241aa, $be0b1010, $c90c2086,
$5768b525, $206f85b3, $b966d409, $ce61e49f,
$5edef90e, $29d9c998, $b0d09822, $c7d7a8b4,
$59b33d17, $2eb40d81, $b7bd5c3b, $c0ba6cad,

$edb88320, $9abfb3b6, $03b6e20c, $74b1d29a,
$ead54739, $9dd277af, $04db2615, $73dc1683,
$e3630b12, $94643b84, $0d6d6a3e, $7a6a5aa8,
$e40ecf0b, $9309ff9d, $0a00ae27, $7d079eb1,

$f00f9344, $8708a3d2, $1e01f268, $6906c2fe,
$f762575d, $806567cb, $196c3671, $6e6b06e7,
$fed41b76, $89d32be0, $10da7a5a, $67dd4acc,
$f9b9df6f, $8ebeeff9, $17b7be43, $60b08ed5,

$d6d6a3e8, $a1d1937e, $38d8c2c4, $4fdff252,
$d1bb67f1, $a6bc5767, $3fb506dd, $48b2364b,
$d80d2bda, $af0a1b4c, $36034af6, $41047a60,
$df60efc3, $a867df55, $316e8eef, $4669be79,

$cb61b38c, $bc66831a, $256fd2a0, $5268e236,
$cc0c7795, $bb0b4703, $220216b9, $5505262f,
$c5ba3bbe, $b2bd0b28, $2bb45a92, $5cb36a04,
$c2d7ffa7, $b5d0cf31, $2cd99e8b, $5bdeae1d,

$9b64c2b0, $ec63f226, $756aa39c, $026d930a,
$9c0906a9, $eb0e363f, $72076785, $05005713,
$95bf4a82, $e2b87a14, $7bb12bae, $0cb61b38,
$92d28e9b, $e5d5be0d, $7cdcefb7, $0bdbdf21,

$86d3d2d4, $f1d4e242, $68ddb3f8, $1fda836e,
$81be16cd, $f6b9265b, $6fb077e1, $18b74777,
$88085ae6, $ff0f6a70, $66063bca, $11010b5c,
$8f659eff, $f862ae69, $616bffd3, $166ccf45,

$a00ae278, $d70dd2ee, $4e048354, $3903b3c2,
$a7672661, $d06016f7, $4969474d, $3e6e77db,
$aed16a4a, $d9d65adc, $40df0b66, $37d83bf0,
$a9bcae53, $debb9ec5, $47b2cf7f, $30b5ffe9,

$bdbdf21c, $cabac28a, $53b39330, $24b4a3a6,
$bad03605, $cdd70693, $54de5729, $23d967bf,
$b3667a2e, $c4614ab8, $5d681b02, $2a6f2b94,
$b40bbe37, $c30c8ea1, $5a05df1b, $2d02ef8d);

function GetCrc32(s: string; Seed: LongInt): string;
var Count: Integer;
CrcVal: LongInt;
begin
CrcVal := Seed;
for Count := 1 to Length(s) do
CrcVal := CrcTbl[Byte(CrcVal xor DWORD(Ord(s[Count])))] xor ((CrcVal shr 8) and $00FFFFFF);
Result := IntToHex(not(CrcVal), 8);
end;

function GetCrc32ForFile(AFileName: string; Seed: LongInt): string;
var
Buf : Array[1..1000] of char;
BufLen : Integer;
Count: Word;
CRCVal: Longint;
hFile : Integer;
BytesRead : Word;

begin
Result := '';
CRCVal := Seed;
hFile := FileOpen(AFileName, 0);
if hFile = -1 then
exit;
try
repeat
BytesRead := FileRead(hFile, Buf, SizeOf(Buf));
if BytesRead > 0 then
for Count := 0 to BytesRead - 1 do
CRCVal := CrcTbl[Byte(CRCVal xor LongInt(Buf[Count]))] xor ((CRCVal shr 8) and $00FFFFFF);
until BytesRead = 0;
Result := IntToHex(not(CRCVal), 8);
finally
FileClose(hFile);
end;
end;

function GetCrc32ForStream(AStream : TMemoryStream; Seed: Longint): String;
var
Buf : Array[1..1000] of char;
BufLen : Integer;
Count: Word;
CRCVal: Longint;

begin
Result := '';
CRCVal := Seed;

repeat
BufLen := AStream.Read(Buf, 1000);
if BufLen > 0 then
for Count := 0 to BufLen - 1 do
CRCVal := CrcTbl[Byte(CRCVal xor LongInt(Buf[Count]))] xor ((CRCVal shr 8) and $00FFFFFF);
until BufLen < 1000;

Result := IntToHex(not(CRCVal), 8);
end;

end.

 
请看: http://www.delphibbs.com/delphibbs/dispq.asp?lid=837424 二楼
 

Similar threads

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