Can you tell me how can I do that? Is there any Freepascal unit that can do this for me? I need that so my program can store binary data in it's XML-based fileformat.
            Asked
            
        
        
            Active
            
        
            Viewed 7,617 times
        
    1
            
            
        - 
                    possible duplicate of [binary-to-base64-delphi](http://stackoverflow.com/questions/5795263/binary-to-base64-delphi/5795440#5795440) – Uwe Raabe Apr 20 '12 at 09:05
- 
                    @Uwe Is that unit available in FPC? – David Heffernan Apr 20 '12 at 10:27
- 
                    @David, as I don't have Lazarus I don't know. That's why I wrote _possible_. – Uwe Raabe Apr 20 '12 at 12:10
1 Answers
9
            
            
        Use the base64 unit and its two classes, TBase64EncodingStream and TBase64DecodingStream.
Here is a simple example:
program demo;
uses Classes, base64;
var
  DecodedStream: TStringStream;
  EncodedStream: TStringStream;
  Encoder: TBase64EncodingStream;
  Output: string;
begin
  DecodedStream := TStringStream.Create('Hello World!');
  EncodedStream := TStringStream.Create('');
  Encoder       := TBase64EncodingStream.Create(EncodedStream);
  Encoder.CopyFrom(DecodedStream, DecodedStream.Size);
  Output := EncodedStream.DataString;
  { Outputs 'SGVsbG8gV29ybGQh' }
  WriteLn(Output);
  DecodedStream.Free;
  EncodedStream.Free;
  Encoder.Free;
end.
And, in the opposite direction:
program demo;
uses Classes, base64;
var
  DecodedStream: TStringStream;
  EncodedStream: TStringStream;
  Decoder: TBase64DecodingStream;
  Output: string;
begin
  EncodedStream := TStringStream.Create('SGVsbG8gV29ybGQh');
  DecodedStream := TStringStream.Create('');
  Decoder       := TBase64DecodingStream.Create(EncodedStream);
  DecodedStream.CopyFrom(Decoder, Decoder.Size);
  Output := DecodedStream.DataString;
  { Outputs 'Hello World!' }
  WriteLn(Output);
  DecodedStream.Free;
  EncodedStream.Free;
  Decoder.Free;
end.
or the shorthands encodestringbase64 and decodestringbase64 (2.4.4+) for non stream based usage:
Uses Base64;
var 
   s : AnsiString;
Begin
  s:=EncodeStringBase64('Hello world!');
  Writeln('Encoded : ',s);
  s:=DecodeStringBase64(s);
  Writeln('Decoded again : ',s);    
end.
 
    
    
        Marco van de Voort
        
- 25,628
- 5
- 56
- 89
 
    
    
        David Heffernan
        
- 601,492
- 42
- 1,072
- 1,490
- 
                    1Yes, it will work with any form of stream. That's really the point of making it stream based. – David Heffernan Apr 20 '12 at 09:44
- 
                    Btw Since 2.4.4 there are also a couple of plain helper functions in that unit that do the stream wrapping for ease of use. – Marco van de Voort Apr 20 '12 at 12:22
- 
                    @marco thanks. I think that is worth an answer if you would be so good. – David Heffernan Apr 20 '12 at 12:29
- 
                    A superb and clear answer! I was tring to find this info myself and stumbled across this. Thanks for including the non-streamed method, too, as that is what I needed to use. – Gizmo_the_Great Oct 17 '12 at 10:26
