问题描述
我正在Delphi 7中使用EncodeString对从源文本文件加载的字符串进行编码.在源文本文件中,每一行都是一条记录.现在,我想使用EncodeString函数(它是Base64函数)对每个行字符串进行编码并写入新的目标文本文件.我的目的是源文本文件中的一行字符串应编码为目标文本文件中的一行加密字符串.但是每行编码为3行加密字符串. 3行有多个换行符.如何删除换行符?
I'am using EncodeString in Delphi 7 to encode strings which load from a source textfile. In the source textfile, every line is one record. Now, I want to use the function EncodeString which is Base64 function to encode every line string and write to a new target textfile. My purpose is one line strings in source textfile should encode to one line encryption strings in target textfile. But every line encode to 3 lines encryption strings. There are several line-breaks made to 3 lines. How can I delete the line feed?
代码:
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文本内容:
this is EncodeString text content:
bGluZSAxOiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLg==
bGluZSAyOiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLg==
bGluZSAzOiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZS
我需要在每个目标文本文件的行尾都使用换行符,以便区分每行.
非常感谢!
推荐答案
这是'encddecd.pas'中EncodeStream
过程(EncodeString
调用)的编码方式.它可能符合 RFC 2045 ( MIME ).摘录:
It is how EncodeStream
procedure (which EncodeString
calls) in 'encddecd.pas' is coded. It probably complies with RFC 2045 (transfer encoding for MIME). An excerpt:
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个字节,在输出缓冲区中会添加一个换行符和一个回车符.
As you can see after every 76 bytes a line feed and a carriage return is appended in the output buffer.
您可以在Indy包中使用EncodeString
代替:
uses
idcodermime
...
Writeln(targetFile, TIdEncoderMIME.EncodeString(memoSource.Lines.Strings[i]));
这篇关于如何在Delphi 7中删除由EncodeString函数编码的字符串中的换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!