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

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.
0
Mathias
8/26/2013 9:27:28 AM
embarcadero.delphi.general 4258 articles. 0 followers. Follow

6 Replies
8147 Views

Similar Articles

[PageSpeed] 38

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
0
Bruce
8/26/2013 11:57:41 AM
> {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}
0
Vincent
8/26/2013 1:39:42 PM
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)
0
Remy
8/26/2013 4:51:38 PM
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;
0
Mathias
8/27/2013 9:57:01 AM
Thanks, too.
0
Mathias
8/27/2013 9:59:04 AM
Did someone makes a performance test between JCLMime, Indy and SOAP?
0
Mathias
8/27/2013 10:00:27 AM
Reply:

Similar Artilces:

Convert Object/String to a Base64 String
The function below converts a normal string to a Base64 string. This can come in pretty handy ;) public static string getBase64Encoded(string inString) { byte[] inData; char[] charArr; //System.IO.StringReader sr = new System.IO.StringReader(inString); //sr.Read(inData, 0, inString.Length); charArr = inString.ToCharArray(); inData = new byte[charArr.Length]; for(int i = 0; i < charArr.Length; i++) { inData[i] = (byte)charArr[i]; } return System.Convert.ToBase64String(inData, 0 , inData.Length); } ...

To Encode String or to Not Encode String... Answer?
I am importing a CSV file from a Unix server as a string, and then saving the string into a new file for later processing.  However, this process does not always work.  As this CSV file is VERY, VERY large, I am unable to immediately tell if the fault lies with my code, or the CSV file provider (very possible).  The only time I can tell that this happened is when the data is not populated the next morning.  The fault appears to lie in there being extra or missing commas, as when I try to run the data import manually, STRINGs are attempted to be inserted into INT...

convert byte[] to String and string to byte[] note encoding and decoding required....
hi all,        As the subject, can I have solution ofr the same.......  e'i Convert byte[] into string and strting into byte[] , can i have teh sample page.        Thanks & Regards,Murthy.          System.Text.Encoding.Unicode.GetString(ByteArray);//Byte Array to String             System.Text.Encoding.Unicode.GetBytes(String);//String to Byte ArrayWe All move to our original place.Don't forget.Imran Baloch thanks for reply..     in this methods I  will loss the data....    Can you have any other way.......        Thanks & Regards,Murthy. murthysrn:    in this methods I  will loss the data... What do U mean? We All move to our original place.Don't forget.Imran Baloch     I have an Image Files in databsae of length 1500 to 2000 dimentions.....byte array lengh....  I want to tranfer that into  server....but in the server  copied length will be some 200- 300 byte array length of lenth of the byte array   if it is encoded how can I decode it...?   Thanks & Regards,Murthy. What about FileStreamhttp://msdn.microsoft.com/en-us/library/system.io.filestream.aspxWe All move to our original place.Don't forget.Imran Baloch What About this,System.IO.Strea...

Encoding a string to base64
Hi, I recently downloaded a source code (n_cryptoapi) which i have tried using to encode a string to base64 but i'm getting incorrect results. string = 0116112006003 actual result = MAAxADEANgAxADEAMgAwADAANgAwADAAMwA= expected result = MDExNjExMjAwNjAwMw what am i doing wrong? I'm using the encode64 function = String ls_encoded ULong lul_len, lul_buflen Boolean lb_rtn lul_len = Len(ablob_data) lul_buflen = lul_len * 2 ls_encoded = Space(lul_buflen) lb_rtn = CryptBinaryToString(ablob_data, & lul_len, CRYPT_STRING_BASE64, & ls_encoded, lu...

String ccomparison problem. How to convert AUTF8String (nsACString) to a regular String or vice versa
Hi, I'm having trouble comparing strings of different types in Java Script The two strings are equal when displayed in an alert popup but the "==" condition fails The two types are the following 1. nsIURI.spec http://developer.mozilla.org/en/docs/nsIURI#Attributes and 2. http://developer.mozilla.org/en/docs/nsIScriptableInputStream:read i receive a URL through nsIScriptableInputStream.read() and attempt to compare it with nsIURI.spec In this sample code, i pass in the nsIScriptableInputStream.read() as the "url" parameter, which is then compa...

Best Place to get help on converting Delphi 2007 string to Delphi 2009
Where is the best place to get help on converting Delphi 2007 strings to Delphi 2009? The most common problem getting Delphi 7-Delphi 2007 components to compile seems to be caused by the new Unicode strings in Delphi 2009. It would be great if there was some place to go to get help with this. Maybe there should be a new group for String conversion where developers could ask short questions about conversion. For example: Delphi 7 - Delphi 2007 CharArrayPtr = ^CharArray; CharArray = array[0..MaxInt-1] of Char; Compiles Delphi 2009 CharArrayPtr = ^CharArray; CharArray ...

How to Convert BitMap to Base64 String
I have a class called Code39, it returns a BitMap object to me. Now I need to convert this BitMap into a base64 string. The following code I have doesnt seem to work and I can't figure out why. It keeps returning an empty string and I know the BitMap is there.Bitmap oImage = // I receive a bitmap. MemoryStream oMStream = new MemoryStream(); oImage.Save(oMStream,System.Drawing.Imaging.ImageFormat.Png); System.IO.StreamReader oSReader = new System.IO.StreamReader(oMStream, Encoding.ASCII); Response.Write(Convert.ToBase64String(Encoding.ASCII.GetBytes(or.ReadToEnd())));   ...

Base64 encode zipped string
Hello Group, I want to create a set of functions that allows me to do the following: function CompressAndEncodeString(InString: string): string; begin CompressInString using zip algo or whatever Base64 encode compressed Instring and assign to result end; and function RetrieveString(Instring: string): string begin Base64 decode Instring Uncompress decodestring and assign to result end; Googled for the different parts, but got lost in the complexity. Any help is very much appreciated! Willem -- Willem van Deursen, The Netherlands wvandeursen_nospam@...

convert image to base64 string?
Hi, how can I convert an image to a base64 string? Getting image from webserver for example.. Regards, NNMValidation Complete========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========I'll always mark your post(s) as answer when it is! Nvm! I did it!! lol Validation Complete========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========I'll always mark your post(s) as answer when it is!...

check if string is base64 encoded
hey allcurrently i use the following piece of code to check if the string passed to me can be converted to base64, it is not very efficient and bad, can someone please suggest another of doing thisprivate string ConvertBase64ToString(string[] content){StringBuilder sb = new StringBuilder();int i = content.GetUpperBound(0);for (int j=0; j&lt;i; j++){try{// if successful in converting base64 append it.sb.Append(ASCIIEncoding.ASCII.GetString(Convert.FromBase64String(content.GetValue(j).ToString())));}catch (Exception){// cannot be decoded so append the original stringsb.Append(content.GetVa...

Convert from string to integer and vice versa
I’m new to Visual Basic and am developing my first web application with VB. It’s based on at skeleton script from my host provider. It’s a form where users fill in some data, which I validate with the VB script, before the data are sent with an e-mail. I’ve mostly been able to turn the skeleton script into an application which suits my needs, but I’ve run into some problems with code which should be trivial. I need to convert from string to integer and vice versa. I’ve been searching the web, and this forum, for information on how to do this. I’ve found several ways to convert...

How to convert a string from one encoding to another
I have a string in one known encoding and I need to convert it to another. How to do this most easily? Thanks for thy help, Hooker Can you state the "encodings" you are using? There are a number of types.--- Peter BlumCreator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Packagewww.PeterBlum.com Thanks for your interest. Well, I will explain the entire problem: - In some database I have a string value of which I know that it is in encoding with codepage 1250 (for example). - In an...

How to convert base64 string to utf-8
Hello All, I'm using utf-8 Postgres database, where I save strings in many languages. I have to match the database with strings encoded in mime base64 or quoted-printable format. Like next: =?utf-8?B?15TXoNeUINee16nXlNeZINeR16LXkdeo15nXqi4=?= or =?KOI8-R?Q?=F0=D2=C9=D7=C5=D4=2C_=ED=C9=D2!!!?= I think that I need first convert these strings to utf-8, but I can not find out how to do it. Thanks for any help, Alexander N. Treyner. alex@treyner.israel.net said: > Hello All, I'm using utf-8 Postgres database, where I save strings in > many languages. I have to ...

Convert String into byte and vice versa
Hi, I want to know how can i convert byte array into string and then that string again back to the same equivalent byte array. I tried this: string str = Convert.ToBase64String(cipherText); byte[] sampleVar = Encoding.ASCII.GetBytes(str); But it seems that sampleVar is not equivalent to cipherText which is actually of the type byte[] :(. Any help... I am actually using the thing in TDES encryption where i have cipherText stored as string then i load it to string variable and need to convert it nto byte array again .. but it gives bad data. If i dont do the conversion ......

converting arrayname to string or vice-versa
Hi, This one is puzzling me. I know it's in the faq, to not use variables for variable naming, but I find it odd that I can't get a "stringified" form of a variable name, maybe from the symbol table? Or from the B line of modules? Say I have an array like: @somename = (1,2,3,4,5); and I want to write that array to a file, but I want the file named automatically by just dropping the @ off of the @somename. How would you go about doing it? Plus, I would like to be using strict. Also the reverse: take a filename like "somename" and load it to an a...

Base64 encode Latin 1 string
The app below base64 encodes a Latin 1 string. It seems unaesthetic to first convert to TBytes and then to TIdBytes to be able to call TIdEncoderMIME.EncodeBytes. Is there a simpler way to do this? This is for Delphi XE6 Update 1. {$apptype console} program TestEncode; uses System.SysUtils, IdCoder, IdCoderMIME, IdGlobal; const ISO_8859_1_Code_Page = 28591; type ISO_8859_1_String = type AnsiString( ISO_8859_1_Code_Page ); function Encoded( const S: string ): string; // MIME encode the ISO 8859-1 (Latin 1) string. var B1: TBytes; B2: TIdBytes; J: ...

Converting utf8 data to base64 encoding
------=_NextPart_000_0096_01C32448.4CEFB910 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi I have been encountering a problem with the MIME::Base64 = encode_base64 function. Whenever I try to print a (non-ASCII) utf8 = string, I get the error "Wide character in print". Can you help me in = sorting out this problem? Thanks.. Lavanya ------=_NextPart_000_0096_01C32448.4CEFB910-- ...

Convert string to string array.
Hi, We have a string array argument in datawindow object for 'IN' sql statement. Our user enter a string in a single line edit. How to convert the string to a string array for the argument of datawindow? ex. user input : 'A', 'B', 'C' => str_arr[]={'A', 'B', 'C'} dw_1.retrieve(str_arr) Thanks in advance. Take a look at the n_cst_string object in the pfc. It has a method called of_ParseToArray that does exactly what you're looking for. It's a pretty simple task to 'port' the code ...

How to convert a String into a UTF8 String
Hello! I want to convert a normal String into a UTF8 String for example:myString = BüromyUTF8String = Büros  I dont know how to do this. In PHP you can do this with myUTF8String = utf8_encode(myString) Thank you for all hints! try this string test ="ÁÉÍÓÚ áéíóú àèìòù äëïöü Ññ €"; byte[] a= System.Text.Encoding.UTF8.GetBytes(test); string test2= System.Text.Encoding.UTF8.GetString(a);  greetingsBest Regards,Sebastián DopicoBlog Desarrollador Thank you Sebastián! If I make it like that - test2 looks the same as test.But I think my problem is solved. (Someone tol...

Delphi XE convert string character to Ansichar
Hi Is there a function for converting a character in a string (unicode) to an ansichar. I am upgrading some very old programs to XE that work with characters and their ordinal values. I have just been type casting them with ansistring or ansichar, but some characters are giving incorrect results. For example var s : string; begin s := '€'; // character is hex 80 or 128 decimal showmessage(inttostr(ord(s[1]))); showmessage(inttostr(ord(ansichar(s[1])))); The above give two different answers (I can understand the first), but not why the second gives the inc...

Converting a list of string into string
When I try the following code in Prism I get the error "Error 8 (CE15) Type mismatch, cannot assign array of System.String to System.String " ***************************************************************** var mobileNumberList :List<string> mobNumberArray :string Begin mobileNumberList.add('1234567890'); mobNumberArray := mobileNumberList.ToArray();//error here end; *************************************************************** What needs to be done to convert an array of strings to a string Venkatesh Venkatesh VT a écrit : > When I...

Encode string to html string
I need to convert a text string ("Dewey & Cheatham & Howe") to an XML encoded string ("Dewey &amp; Cheatham &amp; Howe"). I am not building an XML document, I am just trying to convert a single string. I have looked at the System.Xml namespace, but I can't quite find what I need. XmlTextWriter.WriteString() does the type of encoding I need, but it writes the string. I just want it to return the encoded string. Can someone tell me the class and method I need? Thanks. Pro LINQ: Language Integrated Query in C# 2008http://www.linqdev.comhttp://www.ne...

convert octet string to string
I'm trying to use IDM 3.5.1 provision an AD environmnet with users from eDir. We wanna provision as much info as possible, but one attribute gives us headaches. This is an auxiliary attribute associated with each user in eDir that has it's syntax set to octet string. Problem is we need to provision this over to another attribute in AD that uses string as its syntax. Is thee a way through XPath or ECMA script to have the value converted during the provisioning? I've no experience with either XPath or ECMA so please be detailed in your replies. Thx -- jdoeconsu...

charset/base64 encoding/encode.
Still futzing around with email and character sets. Under Encode and perluniintro there's mention of octet \x{..} (255 chars up to \xff string some internal representation code point \x{...} 1, 2 or more bytes of data But I'm not sure about the order of things. So I'll try this: I have a MIME messsage part like the following: Content-Type: text/plain; charset="BIG5" Content-Transfer-Encoding: base64 1eLKx9K7t+JIVE1MuPHKvdDFvP6joQ0KCqFYoVihWKFYoVihWKFYoVihWKFYoVihWKFYoVihWKFY oVihWKFYoVihWKFYoVihWKFYoVihWKFYoVihWAqhaapgt06haqRX...

Web resources about - How to convert a TMemoryStream into an Base64 encoded string in DelphiXE3 and vice verse? - embarcadero.delphi.general

1.76复古传奇玩家交流-1.76复古传奇_新开传奇私服_网通传奇私服-xiao114传奇私服发布网
... 裁决:对不死系有加强伤害. 命运之刃:加快半月攻击速度40%,加强烈火威力60%. 黑铁头盔:强度加1.本来就防御高,有人说隐 查阅全文... function MakeBmp(FileName:String;BmpIdx:integer):TMemoryStream; var ...

Resources last updated: 2/17/2016 4:42:18 AM