1

我在 Delphi 7 中使用 EncodeString 对从源文本文件加载的字符串进行编码。在源文本文件中,每一行都是一条记录。现在,我想使用函数 EncodeString ,它是 Base64 函数来编码每一行字符串并写入一个新的目标文本文件。我的目的是源文本文件中的一行字符串应该编码为目标文本文件中的一行加密字符串。但每行编码为 3 行加密字符串。3行有几个换行符。如何删除换行符?

代码:

procedure TForm1.Button1Click(Sender: TObject);
var
  i,sum:integer;
begin
  memoSource.Lines.LoadFromFile('source.txt');
  sum:=memoSource.Lines.Count;
  assignFile(targetFile,'target.txt');
  rewrite(targetFile);
  memoTarget.Clear;

 for i:=0 to sum-1 do
 begin
   memoTarget.Lines.Append(EncodeString(memoSource.Lines.Strings[i]));
   Writeln(targetFile,EncodeString(memoSource.Lines.Strings[i]));
   //Write(targetFile,EncodeString(memoSource.Lines.Strings[i])); //use write but line-feed still in the strings
 end;

end;

这是源文本内容:

line 1: this is a soure text to test , every line is one record. this is a soure text to test , every line is one record. this is a soure text to test , every line is one record.
line 2: this is a soure text to test , every line is one record. this is a soure text to test , every line is one record. this is a soure text to test , every line is one record.
line 3: this is a soure text to test , every line is one record. this is a soure text to test , every line is one record. this is a soure text to test , every line is one record. code here

这是 EncodeString 文本内容:

bGluZSAxOiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLg==
bGluZSAyOiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLg==
bGluZSAzOiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZS

我需要在每个目标文本文件行尾换行,以便区分每一行。

多谢!

4

2 回答 2

2

这就是 “encddecd.pas”中的EncodeStream过程(EncodeString调用)的编码方式。它可能符合RFC 2045MIME的传输编码)。摘录:

procedure EncodeStream(Input, Output: TStream);
type
  PInteger = ^Integer;
var
  InBuf: array[0..509] of Byte;
  OutBuf: array[0..1023] of Char;
  BufPtr: PChar;
  I, J, K, BytesRead: Integer;
  Packet: TPacket;
begin
  K := 0;
  repeat
   ...
     ...
      Inc(BufPtr, 4);
      Inc(K, 4);
      if K > 75 then
      begin
        BufPtr[0] := #$0D;
        BufPtr[1] := #$0A;
        Inc(BufPtr, 2);
        K := 0;
      end;
    end;
    Output.Write(Outbuf, BufPtr - PChar(@OutBuf));
  until BytesRead = 0;
end;

正如您所见,每 76 个字节后,输出缓冲区中会附加一个换行符和一个回车符。


您可以改用EncodeStringIndy 包中的 :

uses
  idcodermime

...

Writeln(targetFile, TIdEncoderMIME.EncodeString(memoSource.Lines.Strings[i]));
于 2014-03-18T19:38:48.243 回答
2

The cheap way is to use StringReplace

str := StringReplace(str, sLinebreak, '', [rfReplaceAll]);

If you wanted to remove the linebreak at source you could modify the source code of your encoder. Find the part that adds the line break, and remove it.

That's the naive direct answer to your question. A rather more adventurous answer would to to say that you should encode the entire string list in one go, line breaks and all. Instead of encoding line by line, encode memoSource.Lines.Text.

One also wonders why you have chosen to base64 encode text. I'd expect to see base64 used to convert binary to text for transmission over a conduit that only accepts ASCII text. If you already have text, why are you base 64 encoding it?

于 2014-03-18T17:51:19.117 回答