我需要在Delphi6中将PDF文档编码为base64格式。有人能帮忙吗?
EncdDecd
单元。您需要使用的函数是EncodeStream
。您只需要创建两个流,一个输入流和一个输出流。如果您是在处理文件,则应该创建TFileStream
实例。EncodeStream(InputStream, OutputStream);
unit base64;
interface
uses Classes;
function base64encode(f:TStream):string;
implementation
const
Base64Codes:array[0..63] of char=
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
function base64encode(f:TStream):string;
const
dSize=57*100;//must be multiple of 3
var
d:array[0..dSize-1] of byte;
i,l:integer;
begin
Result:='';
l:=dSize;
while l=dSize do
begin
l:=f.Read(d[0],dSize);
i:=0;
while i<l do
begin
if i+1=l then
Result:=Result+
Base64Codes[ d[i ] shr 2]+
Base64Codes[((d[i ] and $3) shl 4)]+
'=='
else if i+2=l then
Result:=Result+
Base64Codes[ d[i ] shr 2]+
Base64Codes[((d[i ] and $3) shl 4) or (d[i+1] shr 4)]+
Base64Codes[((d[i+1] and $F) shl 2)]+
'='
else
Result:=Result+
Base64Codes[ d[i ] shr 2]+
Base64Codes[((d[i ] and $3) shl 4) or (d[i+1] shr 4)]+
Base64Codes[((d[i+1] and $F) shl 2) or (d[i+2] shr 6)]+
Base64Codes[ d[i+2] and $3F];
inc(i,3);
if ((i mod 57)=0) then Result:=Result+#13#10;
end;
end;
end;
end.
EncdDecd
,它甚至在古老的D6版本中都有提供。 - David Heffernanlinebrake
会很有用。
if ((i mod 57)=0) and linebrake then Result:=Result+#13#10;
- Maxim Ponomarevuses IdCoder, IdCoder3to4, IdCoderMIME, IdBaseComponent;
procedure TForm1.Button1Click(Sender: TObject);
var
SourceStr: TFileStream;
Encoder: TIdEncoderMIME;
begin
if OpenDialog1.Execute then
begin
SourceStr := TFileStream.Create(OpenDialog1.FileName, fmOpenRead);
try
Encoder := TIdEncoderMIME.Create(nil);
try
Memo1.Lines.Text := Encoder.Encode(SourceStr);
finally
Encoder.Free;
end;
finally
SourceStr.Free;
end;
end;
end;