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
Comments
-
Kermia over 3 years
How can I get content of an
exe
file and convert it intoBase64
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 about 13 yearsI 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 about 13 yearsDid you look to see if D2010 includes the
EncdDecd
unit? @Uwe's code shows you exactly how to use it. -
Uwe Raabe about 13 yearsThe unit is also available in D2010. I edited my answer to include an example.
-
Warren P about 13 yearsFor those using ancient Delphi versions, equivalent functions are found somewhere in the Indy's Mime Encoding stuff.
-
Remy Lebeau about 13 years@Warren: specifically, Indy's IdCoderMIME unit contains base64-enabled classes.
-
Ken Bourassa about 13 yearsThere's also the CryptBinaryToString function from Crypt32.dll (WinXP+) for those with older Delphi version.
-
Uwe Raabe almost 13 yearsYou 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 about 12 yearsSynapse doesn't come with any version of Windows. Maybe you meant indy?
-
drakorg almost 12 yearsBe 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 about 11 yearsThis answer just saved my life for sending binary files via JSON :D
-
Rod Lima almost 11 years@Marco van de Voort. No, I mean synapse. Look at the link.
-
Marco van de Voort almost 11 yearsMy bad, I meant that synapse doesn't come packaged with Delphi, indy does.
-
Dave Nottage over 8 yearsIn later versions of Delphi (not sure where it started): uses System.NetEncoding; TNetEncoding.Base64.EncodeBytesToString(Stream.Memory, Stream.Size)
-
Bourgui over 7 yearsFYI, the TNetEncoding mentioned by DaveNottage was added in XE7, as I just found out. Use EncdDecd with earlier versions.