• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9846
  • Last Modified:

Decode Base64 String to Byte Array and viceversa in Delphi 7

I found this link but it is applicable for D2009 onwards not Delphi 7:
http://stackoverflow.com/questions/8768652/base64-to-binary-delphi

Basically my requirement is these two methods: (Shown as Pseudocode)
resultBase64String := EncodeBase64(bytearray,  byteArraySize);
resultByteArray := DecodeBase64(base64String);
with some sample code on how to use it, the string to decode could be: "1234"
0
Miguel Oz
Asked:
Miguel Oz
  • 3
  • 3
2 Solutions
 
Sinisa VukCommented:
loo for functions here:
http://www.delphipraxis.net/991-base64-mime-en-decoding.html
http://www.swissdelphicenter.ch/torry/showcode.php?id=1524

...and usage is very simple:

(for delphipraxis implementation)
...
  s := '1234';
...
 var
  InSize, OutSize: Cardinal;
  PIn, POut: Pointer;
...
  InSize := Length(s);
  OutSize := CalcEncodedSize(InSize);
  SetLength(OutText, OutSize);
  PIn := @s[1];
  POut := @OutText[1];
  Base64Encode(PIn, InSize, POut);
...

Open in new window


(for swissdelphicenter example)
...
  s := Encode64('1234');
  s2: = Decode64(s); //result '1234'
...

Open in new window

0
 
Miguel OzSoftware EngineerAuthor Commented:
Thanks but the examples above are returning strings only.
Apologies that my question was not clear enough in the pseudocode, my requirement is:

var
   array: TByteDynArray;//array of byte
   original, s2: string;
begin
   original = '1234';
   array = Encode64StringToByteArray(original);;//returns array of byte
   s2: = DecodeByteArrayTo64String(array ); //result '1234' = original
end;

Open in new window


Reason: I need to to transmit a byte array but the SOAP implementions is broken, thus I need to transmit the byte array as string and then convert it back to byte array.
0
 
Sinisa VukCommented:
using unit from http://www.delphipraxis.net/991-base64-mime-en-decoding.html
I made little example for your needs.

uses ...,Types,Base64;
...
function Encode64StringToByteArray(const InText: AnsiString): TByteDynArray;
var
  InSize, OutSize: Cardinal;
  PIn, POut: Pointer;
begin
  InSize := Length(InText);
  PIn := @InText[1];
  OutSize := CalcDecodedSize(PIn, InSize);

  SetLength(Result, OutSize);
  FillChar(Result[0], OutSize, $FF);
  POut := @Result[0];

  if not Base64Decode(PIn, InSize, POut) then
    SetLength(Result, 0);
end;

function DecodeByteArrayTo64String(const arr: TByteDynArray): String;
var
  InSize, OutSize: Cardinal;
  PIn, POut: Pointer;
begin
  InSize := Length(arr);
  OutSize := CalcEncodedSize(InSize);

  SetLength(Result, OutSize);
  PIn := @arr[0];
  POut := @Result[1];

  Base64Encode(PIn, InSize, POut);
end;
...

Open in new window


... and think that your request is more like ...

...
procedure TForm1.Button1Click(Sender: TObject);
var
  BArray: TByteDynArray;//array of byte
  s2: string;
begin
  SetLength(BArray, 3);
  BArray[0] := $45;
  BArray[1] := $18;
  BArray[2] := $27;

  s2 := DecodeByteArrayTo64String(BArray);
  BArray := Encode64StringToByteArray(s2);
end;
...

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Miguel OzSoftware EngineerAuthor Commented:
I tested this code for bigger arrays and it is working OK converting back/to array, but I notice that the Base 64 string does not incluce line breaks every 76  bytes  as per here.

Any particular reason to do that.
0
 
Sinisa VukCommented:
Break lines is just visible feature. You can do it manually...

...
sBase64WithBreaks := WrapText(sBase64, #13#10, [], 76);
...
sBase64 := StringReplace(sBase64WithBreaks, #13#10, '', [rfReplaceAll]);
...

Open in new window

0
 
Miguel OzSoftware EngineerAuthor Commented:
Excellent answer and this code is far quicker than the existing variant based one.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now