问题
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?
Code:
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;
this is source text content:
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
this is EncodeString text content:
bGluZSAxOiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLg==
bGluZSAyOiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLg==
bGluZSAzOiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZSBpcyBvbmUg
cmVjb3JkLiB0aGlzIGlzIGEgc291cmUgdGV4dCB0byB0ZXN0ICwgZXZlcnkgbGluZS
I need a line-break in every target textfile line end so I can distinguish every line.
thanks a lot!
回答1:
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?
回答2:
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;
As you can see after every 76 bytes a line feed and a carriage return is appended in the output buffer.
You can use the EncodeString
in the Indy package instead:
uses
idcodermime
...
Writeln(targetFile, TIdEncoderMIME.EncodeString(memoSource.Lines.Strings[i]));
来源:https://stackoverflow.com/questions/22487059/how-to-delete-line-break-in-string-which-is-encoded-by-the-function-encodestring