Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi - Array[0..693] of Byte - to B64 Encoded String

I have a declared variable in my code like so:

Var
   VarName:Array[0..693] of Byte = ( and my array here );

I have EncdDecd in my uses..

I am looking to encode this Array of Byte into a base64 string using the EncodeBase64 function in EncdDecd.pas

But I am unsure of how to return it into a nice and pretty b64 string that can be converted directly back into a byte array with DecodeBase64...

I have tried a few different approaches..

Var Res:PWideChar;
begin
    StringToWideChar(EncodeBase64(@VarName, 693), Res, 693);
    ClipBoard.SetTextBuf(Res);
end;

Access Violation with that code...

Also tried:

begin
    ClipBoard.SetTextBuf(PWideChar(EncodeBase64(@VarName, 693)));
end;

Which returns a string full of distorted Chinese symbols....

Any help on returning this string would be greatly appreciated..

Thanks!

like image 577
Josh Line Avatar asked Oct 20 '25 07:10

Josh Line


1 Answers

The functions are declared as

function  DecodeBase64(const Input: AnsiString): TBytes;
function  EncodeBase64(const Input: Pointer; Size: Integer): AnsiString;

so all you need in Unicode Delphi's is to cast AnsiString to string,

var S: string;

begin
  S:= string(EncodeBase64(@VarName, 693));
  ..

to decode S you should cast it to AnsiString:

var B: TBytes;

begin
  B:= DecodeBase64(AnsiString(S));
  ..
like image 94
kludg Avatar answered Oct 21 '25 22:10

kludg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!