Unit CRC8.pas
unit Crc8;
interface
{unit Crc8.pas}
Uses
Classes, Windows;
Function Crc_8n(p : array of BYTE; len : BYTE) : Byte;
implementation
Function Crc_8n(p : array of BYTE; len : BYTE) : Byte;
Var
j, cbit, aout, crc, crc_a, crc_b : Byte;
i : integer;
begin
crc := 0;
i := 0;
// Take the shifted bit
repeat
crc_a := p[i];
inc(i);
j := 8;
cbit := 1;
repeat
crc_b := crc_a;
crc_b := crc_b xor crc;
aout := crc_b and cbit;
if aout<>0 then begin
crc := crc xor $18;
crc := crc shr 1;
crc := crc or $80;
end else begin
crc := crc shr 1;
end;
crc_a := crc_a shr 1;
dec(j);
until j = 0;
dec(len);
until len = 0;
result := crc;
end;
end.
Funktion:
uses CRC8
const MinBase = 2;
MaxBase = 36;
//
Function strtonum (const S: string; base: integer;
Neg: Boolean; max: integer): integer;
var negate, done: Boolean;
I, Len, digit, MMB: integer;
C: Char;
Mdb, Res: integer;
Begin
Res := 0; I:= 1; digit:= 0;
If (base >= minbase) and (base <= maxbase) then begin
MMB:= max mod base;
MDB:= max Div base;
Len:= length (s );
Negate:= false;
While (I <= Len) and (S [I] = '') Do Inc (I );
If neg then begin
Case s[I] of '+': Inc (I );
'-': Begin Inc (I); negate:= true; end;
End; (* case *)
End; (* If neg *)
Done:= Len> I;
While (I <= Len) and done do begin
C:= upcase (s [I]);
Case C of
'0' .. '9': digit:= ord (c)-48;
'A' .. 'Z': digit:= ord (c)-55;
Else done:= false
End; (* case *)
Done:= Done And (digit <base );
If done then begin
Done:= (RES <MDB) or (RES = MDB) and (digit <= MMB );
If done then begin
Res:= res * base + digit;
INC (I );
End; (* if done *)
End; (* if done *)
End; (* While *)
If negate then res:=-res;
End; (* if done *)
Result:= res;
End;
Beispiel :
procedure TForm1.Button1Click(Sender: TObject);
Var S : String; P : Array[0..255] of Byte;
Len : Byte; R : Byte; I : Integer;
begin
S := Edit1.Text;
if length(s) mod 2 = 1 then s := s + '0';
Memo1.Lines.Add(S + ':');
for i:=1 to length(s) div 2 do begin
p[i-1] := BYTE(StrToNum(copy(s, (i-1)*2 + 1, 2), 16, false, 500));
Memo1.Lines.Add(IntToStr(I) + '--> ' + IntToHex(p[i-1], 2));
end;
Len := length(s) div 2;
R := Crc_8n(P, Len);
Memo1.Lines.Add('Crc8 Result:' + IntToHex(R, 2));
end;
Keine Kommentare:
Kommentar veröffentlichen