Mega Search
23.2 Million


Sign Up

Make a donation  
How to convert a TMemoryStream into an Base64 encoded string  
News Group: embarcadero.public.delphi.language.delphi.general

How to convert a TMemoryStream into an Base64 encoded string in DelphiXE3 
and vice verse?

I know JCLMime.pas, but it works only from Stream to Stream or from 
ANSISTRING to ANSISTRING.

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 26-Aug-2013, at 2:27 AM EST
From: Mathias Pannier
 
Re: How to convert a TMemoryStream into an Base64 encoded st  
News Group: embarcadero.public.delphi.language.delphi.general
Did someone makes a performance test between JCLMime, Indy and SOAP?

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 27-Aug-2013, at 3:00 AM EST
From: Mathias Pannier
 
Re: How to convert a TMemoryStream into an Base64 encoded st  
News Group: embarcadero.public.delphi.language.delphi.general
Thanks, too.

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 27-Aug-2013, at 2:59 AM EST
From: Mathias Pannier
 
Re: How to convert a TMemoryStream into an Base64 encoded st  
News Group: embarcadero.public.delphi.language.delphi.general
Thanks for TStringStream.

I'm using now this functions:

  function StreamToMimeString(const aStream: TMemoryStream): string;
  var
    aOutput: TStringStream;
  begin
    aOutput := TStringStream.Create;
    try
      //EncodeStream(aStream, aOutput);            //Soap.EncdDecd including 
LineBreaks
      MimeEncodeStreamNoCRLF(aStream, aOutput);    //JclMime no LineBreaks
      Result := aOutput.DataString;
    finally
      aOutput.Free;
    end;
  end;

  function MimeStringToStream(const aString: string): TMemoryStream;
  var
    aInput: TStringStream;
  begin
    Result := TMemoryStream.Create;
    aInput := TStringStream.Create(aString);
    try
      //DecodeStream(aInput, Result);
      MimeDecodeStream(aInput, Result);
    finally
      aInput.Free;
    end;
  end;

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 27-Aug-2013, at 2:57 AM EST
From: Mathias Pannier
 
Re: How to convert a TMemoryStream into an Base64 encoded st  
News Group: embarcadero.public.delphi.language.delphi.general
Mathias wrote:

> How to convert a TMemoryStream into an Base64 encoded
> string in DelphiXE3 and vice verse?

Indy 10 ships with Delphi, and has TIdEncoderMIME and TIdDecoderMIME classes 
for working with base64:

{code:delphi}
uses
  ..., IdCoderMIME;

var
  S: String;
  Stream: TMemoryStream;
....
  S := TIdEncoderMIME.EncodeStream(Stream);
{code}

{code:delphi}
uses
  ..., IdCoderMIME;

var
  S: String;
  Stream: TMemoryStream;
....
  TIdDecoderMIME.DecodeStream(S, Stream);
{code}

--
Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 26-Aug-2013, at 9:51 AM EST
From: Remy Lebeau (TeamB)
 
Re: How to convert a TMemoryStream into an Base64 encoded st  
News Group: embarcadero.public.delphi.language.delphi.general
Mathias Pannier wrote:

> How to convert a TMemoryStream into an Base64 encoded string in
> DelphiXE3 and vice verse?
> 
> I know JCLMime.pas, but it works only from Stream to Stream or from
> ANSISTRING to ANSISTRING.

The built in functions do the same.  Stream to stream or string to
string.

I would do it in two steps.  1) Encode the stream and 2) read from the
destination stream into a string.

-- 
Regards,
Bruce McGee
Glooscap Software

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 26-Aug-2013, at 4:57 AM EST
From: Bruce McGee
 
Re: How to convert a TMemoryStream into an Base64 encoded st  
News Group: embarcadero.public.delphi.language.delphi.general
> {quote:title=Mathias Pannier wrote:}{quote}
> How to convert a TMemoryStream into an Base64 encoded string in DelphiXE3 
> and vice verse?
> 
> I know JCLMime.pas, but it works only from Stream to Stream or from 
> ANSISTRING to ANSISTRING.

Hi, 

Something like this ?

{code}
uses encdDecd;

{$R *.dfm}

procedure TForm19.Button1Click(Sender: TObject);
var MyString : String;
    a : TStringStream;
    b : TStringStream;
begin
  a :=  TStringStream.Create;
  b := TStringStream.Create;

  a.WriteString('Hello World');
  a.Position := 0;
  b.Clear;

  EncodeStream(a,b);

  ShowMessage('base64 Encoded : ['+b.DataString+']');

  a.Clear;
  b.Position := 0;
  DecodeStream(b,a);

  ShowMessage('base64 Decoded : ['+a.DataString+']');
end;
{code}

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 26-Aug-2013, at 9:39 AM EST
From: Vincent Gsell