Home > Uncategorized > Encode/Decode base64 with Delphi and PHP

Encode/Decode base64 with Delphi and PHP

December 10th, 2009 admin Leave a comment Go to comments

Sometime i need send/receive data between Delphi and PHP. To prevent unneeded characters i use Base64 to encode and decode data. Here is code for PHP and Delphi, it work fine with Unicode.

[sourcecode language='php']

$mydata=”Welcome – Chào các bạn.”;
echo base64_encode($mydata);
/* return V2VsY29tZSAtIENow6BvIGPDoWMgYuG6oW4u */

[/sourcecode].

Decode data receive from PHP in Delphi
[sourcecode language='Delphi']

uses EncdDecd;
….

procedure TForm1.Button2Click(Sender: TObject);
var
str, strEncode, strDecode: string;
begin

(* code to encode to base64 in Delphi
strEncode := EncodeBase64(BytesOf(UTF8Encode(str)), Length(BytesOf(UTF8Encode(str))));
*)

str:=’V2VsY29tZSAtIENow6BvIGPDoWMgYuG6oW4u’;
strDecode := UTF8ToUnicodeString(StringOf(DecodeBase64(str)));
ShowMessage(strDecode);
(* result will “Welcome – Chào các bạn.” *)
end;
[/sourcecode].

Categories: Uncategorized Tags:
  1. No comments yet.