简体   繁体   English

二进制到 Base64 (Delphi)

[英]Binary to Base64 (Delphi)

How can I get content of an exe file and convert it into Base64 encoding?如何获取exe文件的内容并将其转换为Base64编码?

Edit编辑

I use D2010 and I want to know how is it possible exactly?我用D2010 ,我想知道到底怎么可能?

  • open an exe file
  • convert its content into base64

In Delphi 2009/2010/XE there is unit EncdDecd.pas ( Soap.EncdDecd.pas for Delphi XE2) containing the functions EncodeBase64 and DecodeBase64 .在 Delphi 2009/2010/XE 中有一个单元EncdDecd.pasSoap.EncdDecd.pas for Delphi XE2)包含函数EncodeBase64DecodeBase64 You can load the exe file into a memorystream and then call EncodeBase64.您可以将 exe 文件加载到内存流中,然后调用 EncodeBase64。

function EncodeFile(const FileName: string): AnsiString;
var
  stream: TMemoryStream;
begin
  stream := TMemoryStream.Create;
  try
    stream.LoadFromFile(Filename);
    result := EncodeBase64(stream.Memory, stream.Size);
  finally
    stream.Free;
  end;
end;

In ancient Delphi versions, you can use synapse ( link here )在古老的 Delphi 版本中,您可以使用synapse链接在这里

Just put synacode.pas in your uses e call EncodeBase64/EncodeBase64.只需将synacode.pas放在您的用途中即可调用 EncodeBase64/EncodeBase64。

Cheers干杯

As also mentioned in the comments, since Delphi XE8 you can use the System.NetEncoding.TNetEncoding.Base64 class property.如评论中所述,自 Delphi XE8 起,您可以使用System.NetEncoding.TNetEncoding.Base64 class 属性。
It also returns a string instead of an AnsiString :它还返回一个string而不是AnsiString

function TryEncodeFile(const AFileName: string; out ABase64string: string): Boolean;
var
  MemStream: TMemoryStream;
begin
  MemStream := TMemoryStream.Create;
  try
    MemStream.LoadFromFile(AFileName);
    ABase64string :=
      TNetEncoding.Base64.EncodeBytesToString(MemStream.Memory, MemStream.Size);
    Result := True;
  finally
    MemStream.Free;
  end;
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM