1

有没有一种优雅的方法可以用 Delphi (6) 删除文件中的奇偶校验位?在这种情况下,奇偶校验位是每第 9 位。

4

2 回答 2

4

假设您的文件是一个包含 9 位块的长位流,并且您希望输出相同的流但具有 8 位块(即每 9 位丢弃一次)。

您可以一次读取 9 个字节(72 位 = 8 个 9 位块),然后使用位移将它们放入 8 个 8 位块中。

您需要一些特殊处理来处理不是 9 字节的倍数的文件,所以这只是一个粗略的指南。

procedure TForm1.Button1Click(Sender: TObject);
var
  FSIn: TFileStream;
  FSOut: TFileStream;
  InBuffer: array[0..8] of Byte;
  OutBuffer: array[0..7] of Byte;
  X: Integer;
  BytesRead: Integer;
  BytesToWrite: Integer;
begin
  FSIn := TFileStream.Create('Input.dat', fmOpenRead);
  FSOut := TFileStream.Create('Output.dat', fmCreate);
  try
    for X := 1 to FSIn.Size div 9 do
    begin
      FillChar(InBuffer[0], 9, 0);
      BytesRead := FSIn.Read(InBuffer[0], 9);
      OutBuffer[0] := InBuffer[0];
      OutBuffer[1] := (InBuffer[1] and 127) shl 1 + (InBuffer[2] and 128) shr 7;
      OutBuffer[2] := (InBuffer[2] and 63) shl 2 + (InBuffer[3] and 192) shr 6;
      OutBuffer[3] := (InBuffer[3] and 31) shl 3 + (InBuffer[4] and 224) shr 5;
      OutBuffer[4] := (InBuffer[4] and 15) shl 4 + (InBuffer[5] and 240) shr 4;
      OutBuffer[5] := (InBuffer[5] and 7) shl 5 + (InBuffer[6] and 248) shr 3;
      OutBuffer[6] := (InBuffer[6] and 3) shl 6 + (InBuffer[7] and 252) shr 2;
      OutBuffer[7] := (InBuffer[7] and 1) shl 7 + (InBuffer[8] and 254) shr 1;

      if BytesRead < 9 then
      begin
        // To do - handle case where 9 bytes could not be read from input
        BytesToWrite := 8;
      end else
        BytesToWrite := 8;

      FSOut.Write(OutBuffer[0], BytesToWrite);
    end;
  finally
    FSIn.Free;
    FSOut.Free;
  end;
end;
于 2009-06-09T16:15:05.310 回答
0

假设您可以将事物一对一地读入整数。

我 := 我 xor 512;

于 2009-06-10T08:12:15.477 回答