Ver Mensaje Individual
  #18  
Antiguo 05-03-2020
Avatar de dec
dec dec is offline
Moderador
 
Registrado: dic 2004
Ubicación: Alcobendas, Madrid, España
Posts: 13.107
Reputación: 34
dec Tiene un aura espectaculardec Tiene un aura espectacular
Hola a todos,

Aquí los intentos que llevo a cabo, usando tanto el código de escafandra como el de ecfisa, dando ambos el mismo resultado: 22586, pero, no el esperado: 5320, la madre que le parió.

Código Delphi [-]
function StrToBytes(const Value: string): TBytes;
var
  I: integer;
begin
  SetLength(Result, Length(Value));
  Move(Value[1], Result[0], Length(Value));
end;

function WordToBytes(const Value: Word): TBytes;
begin
 SetLength(Result, 2);
 Result[0] := TBytes(@Value)[0];
 Result[1] := TBytes(@Value)[1];
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  x, z: Word;
  crc: Word;
  count, i: Integer;
  Input: string;
  Data, Result: TBytes;
begin
  Input := '13|0000123|1|Maximilian|Mustermann|05051999|21092019';
  
  Data := StrToBytes(Input);

  x := 0;
  crc := $1D0F;
  count := Pred(Length(Data));
  while (count >= 0) do
  begin
    Dec(count);
    Inc(x);
    z   := Word(Data[x]) shl 8;
    crc := crc xor z;
    for i := 7 downto 0 do
      if crc and $8000 <> 0 then
        crc := (crc shl 1 xor $1021)
      else
        crc := (crc shl 1);
  end;

  Result := WordToBytes(crc);

  ShowMessageFmt('%d%d', [Result[0], Result[1]]);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  S: TBytes;
  i, x: Integer;
  z: WORD;
  CRC: WORD;
  SLength, n: Integer;
  Result: TBytes;
  Input: string;
begin
  Input := '13|0000123|1|Maximilian|Mustermann|05051999|21092019';

  S := StrToBytes(Input);

  CRC := $1D0F;
  SLength := Length(S);
  X := 1;
  while SLength > 0 do
  begin
    Z := S[X] shl 8;
    CRC := CRC xor Z;
    i:= 8;
    repeat
      if (CRC and $08000) <> 0 then
        CRC := (CRC shl 1) xor $1021
      else
        CRC := (CRC shl 1);
      dec(i);
    until i <= 0;
    Inc(X);
    Dec(SLength);
  end;

  Result := WordToBytes(CRC);

  ShowMessageFmt('%d%d', [Result[0], Result[1]]);
end;
__________________
David Esperalta
www.decsoftutils.com
Responder Con Cita