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

最佳答案

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

您可以一次读取9个字节(72位=八个9位块),然后使用移位将它们放入八个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;

关于file - 使用Delphi从文件中删除奇偶校验位(每9位),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/970155/

10-09 07:13