Binary to Base64 (Delphi)

63,456

Solution 1

In Delphi 2009/2010/XE there is unit EncdDecd.pas (Soap.EncdDecd.pas for Delphi XE2) containing the functions EncodeBase64 and DecodeBase64. You can load the exe file into a memorystream and then call 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;

Solution 2

In ancient Delphi versions, you can use synapse (link here)

Just put synacode.pas in your uses e call EncodeBase64/EncodeBase64.

Cheers

Share:
63,456
Kermia
Author by

Kermia

Near ... Far .. Wherever you are !

Updated on December 02, 2020

Comments

  • Kermia
    Kermia over 3 years

    How can I get content of an exe file and convert it into Base64 encoding ?

    Edit

    I use D2010 and I want to know how is it possible exactly ?

    • open an exe file
    • convert its content into base64
  • Kermia
    Kermia about 13 years
    I use D2010 and i want to know how is it possible exactly ? 1-open an exe file 2-convert its content into base64 ?
  • Ken White
    Ken White about 13 years
    Did you look to see if D2010 includes the EncdDecd unit? @Uwe's code shows you exactly how to use it.
  • Uwe Raabe
    Uwe Raabe about 13 years
    The unit is also available in D2010. I edited my answer to include an example.
  • Warren  P
    Warren P about 13 years
    For those using ancient Delphi versions, equivalent functions are found somewhere in the Indy's Mime Encoding stuff.
  • Remy Lebeau
    Remy Lebeau about 13 years
    @Warren: specifically, Indy's IdCoderMIME unit contains base64-enabled classes.
  • Ken Bourassa
    Ken Bourassa about 13 years
    There's also the CryptBinaryToString function from Crypt32.dll (WinXP+) for those with older Delphi version.
  • Uwe Raabe
    Uwe Raabe almost 13 years
    You mean, you want to know how to save an AnsiString to a file? I guess that counts as a separate question...
  • Marco van de Voort
    Marco van de Voort about 12 years
    Synapse doesn't come with any version of Windows. Maybe you meant indy?
  • drakorg
    drakorg almost 12 years
    Be aware of the fact that EncodeBase64 from the EncdDecd.pas unit will insert 2 AnsiChar's (#$D#$A == CRLF) every 75 base64 encoded chars. You have no way of telling it not to do it. At least the java version has a boolean to control if you want this additional "intelligence". Hope it helps.
  • Jerry Dodge
    Jerry Dodge about 11 years
    This answer just saved my life for sending binary files via JSON :D
  • Rod Lima
    Rod Lima almost 11 years
    @Marco van de Voort. No, I mean synapse. Look at the link.
  • Marco van de Voort
    Marco van de Voort almost 11 years
    My bad, I meant that synapse doesn't come packaged with Delphi, indy does.
  • Dave Nottage
    Dave Nottage over 8 years
    In later versions of Delphi (not sure where it started): uses System.NetEncoding; TNetEncoding.Base64.EncodeBytesToString(Stream.Memory, Stream.Size)
  • Bourgui
    Bourgui over 7 years
    FYI, the TNetEncoding mentioned by DaveNottage was added in XE7, as I just found out. Use EncdDecd with earlier versions.