프로그래밍/델파이
CCITT CRC16() 함수
채윤아빠
2021. 9. 14. 09:11
pascal에 적용 할 수 있는 CCITT CRC16 함수를 소개해 드립니다.
unit crc16Unit;
interface
uses Classes, SysUtils;
const
CCITT16_INITIAL = $FFFF;
CCITT16_POLYNOMINAL = $1021;
{**
CCITT CRC16 값을 계산하여 반환합니다.
@param pBuffer CRC16 값을 계산할 데이터가 들어 있는 포인터
@param nLen CRC16 값을 계산할 데이터의 길이
@result 입력한 데이터에 대한 CCITT CRC16 값 ( 검증 : https://crccalc.com/ )
*}
function crc16(pBuffer: PByte; nLen: word): word; overload;
function crc16(pBuffer: PByte; nLen: word; Polynom, Initial: word): word; overload;
implementation
function crc16(pBuffer: PByte; nLen: word): word; overload;
begin
Result := crc16(pBuffer, nLen, CCITT16_POLYNOMINAL, CCITT16_INITIAL);
end;
function crc16(pBuffer: PByte; nLen: word; Polynom, Initial: word): word;
var
i, j: Integer;
begin
Result := Initial;
for i := 1 to nLen do
begin
Result := Result xor (pBuffer^ shl 8);
for j := 0 to 7 do
begin
if ((Result and $8000) <> 0) then
Result := (Result shl 1) xor Polynom
else
Result := Result shl 1;
end;
Inc(pBuffer);
end;
end;
end.
실행 결과 검증은 https://crccalc.com/ 에서 하시면 됩니다.
참고자료 : https://stackoverflow.com/questions/5139480/
CRC-CCITT (0xFFFF) function?
Can someone help me with Delphi implementation of CRC-CCITT (0xFFFF)? Already get the Java version, but confusing on how to port it to Delphi public static int CRC16CCITT(byte[] bytes) { int ...
stackoverflow.com
728x90
반응형