LRC checksum in Delphi
I am trying to convert a C code to Delphi/pascal, but somehow I cannot get it to work, my approach was like this :
function CheckLRC(s : PChar) : Char;
var
sum : char;
begin
sum := #0;
while (^s <> #0) do
begin
sum := (sum XOR ^s);
inc (s)
end;
result := sum;
end;
Original C code was :
Set LRC = 0
For each byte b in the buffer
do
Set LRC = (LRC + b) AND 0xFF
end do
Set LRC = (((LRC XOR 0xFF) + 1) AND 0xFF)
开发者_Go百科Hope anyone out there is able to help me, and can confirm this is the correct way to do an LRC checksum ?
I guess the exact equivalence of this function may be:
function CheckLRC(const s: AnsiString): byte;
var i: integer;
begin
result := 0;
for i := 1 to length(s) do
inc(result,ord(s[i]));
result := (result xor $FF)+1; // or result := (not result)+1;
end;
All "and $FF" in original code is unneeded here, since we are using a byte
as result.
精彩评论