Тема: CRC24
Показать сообщение отдельно
  #5  
Старый 19.04.2020, 17:10
rodionov_uv rodionov_uv вне форума
Прохожий
 
Регистрация: 23.01.2017
Сообщения: 9
Версия Delphi: Delphi7
Репутация: 10
По умолчанию

Все спасибо, разобрался. Вот полностью рабочий код может кому пригодится.

Код:
unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

function crc_octets (octets: PChar; len: Integer): Integer;
const
CRC24_INIT = $00b704ce;
CRC24_POLY = $01864cfb;
var
crc: Integer;
temp: Integer;
i: Integer;
begin
crc := CRC24_INIT;
while (len <> 0) do
begin
   Dec(len);
   temp := Integer (octets^);
   Inc(octets);
   temp := temp shl 16;
   crc := crc xor temp;
   for i := 0 to 7 do
   begin
     crc := crc shl 1;
     if (crc and $01000000) <> 0 then
       crc := crc xor CRC24_POLY;
   end;
end;
crc := crc and $00ffffff;
result := crc;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
WritteBuffer: array of Byte;
i, k, len: Integer;
j: Real;
s: String;
x: Byte;
begin
  if (Edit1.Text = '') then
  begin
    ShowMessage('У вас заполнены не все поля');
    Exit;
  end
  else
  begin
    Edit2.Text := '';
    len := Length(Edit1.Text);
    if len mod 2 = 0 then
    begin
      j := len / 2;
      len := Round(j);
      SetLength(WritteBuffer, len);
      if RadioGroup1.ItemIndex = 0 then
      Edit2.Text := IntToHex(crc_octets(Addr(Edit1.Text[1]) , len*2), 6)
      else
        begin
          k := 1;
          s := '';
          for i := 0 to len-1 do
          begin
            s := '$' + Copy(Edit1.Text, k, 2);
            x := StrToInt(Copy(s, 1, 3));
            WritteBuffer[i] := x;
            k := k + 2;
          end;
          Edit2.Text := IntToHex(crc_octets(PChar(WritteBuffer), len), 6);
        end;
      end
    else
      begin
        ShowMessage('Не коректно введены данные');
        Exit;
      end;
    end;
end;

end.

Сама прога
CRC24.rar
Ответить с цитированием