CRC-CCITT (0xFFFF) delphi實現


由於要和java的服務溝通,最終確定要實現的是:

width=16

poly=0x1021

init=0xffff

refin=false

refout=false

xorout=0x0000

check=0x29b1

residue=0x0000

name="CRC-16/CCITT-FALSE"

 

網絡找了很多,結果結果對不上

下載了工具,都不是想要的,最終找到校驗在線網址:

 

https://www.lammertbies.nl/comm/info/crc-calculation.html

 

 

實現代碼:

function CRC16_CCITT_0xFFFF(bytes: TBytes): Word;
const
polynomial = $1021;
var
crc: Word;
I, J: Integer;
b: Byte;
bit, c15: Boolean;
begin
crc := $FFFF; // initial value
for I := 0 to High(bytes) do
begin
b := bytes[I];
for J := 0 to 7 do
begin
bit := (((b shr (7-J)) and 1) = 1);
c15 := (((crc shr 15) and 1) = 1);
crc := crc shl 1;
if (c15 xor bit) then crc := crc xor polynomial;
end;
end;

Result := crc and $ffff;
end;

 

當然如果字符串過長,則需要查表法實現,

https://www.libcrc.org/download/

給了c的實現。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM