Encode base64 and Decode base64 using delphi 2007

delphi base64 encode
delphi encode/decode string
base64 encode private key
file to base64
binary to base64
base64 encode api
delphi tnetencoding
base64 algorithm

I have to encode an array of bytes to a base64 string (and decode this string) on an old Delphi 2007. How could I do?

Further Informations:

I've tried synapse (As suggested here Binary to Base64 (Delphi)).

Indy ships with Delphi, and has TIdEncoderMIME and TIdDecoderMIME classes for handling base64. For example:

uses
  ..., IdCoder, IdCoderMIME;

var
  Bytes: TIdBytes;
  Base64String: String;
begin
  //...
  Bytes := ...; // array of bytes
  //...
  Base64String := TIdEncoderMIME.EncodeBytes(Bytes);
  //...
  Bytes := TIdDecoderMIME.DecodeBytes(Base64String);
  //...
end;

There are also methods for encoding/decoding String and TStream data as well.

Update: alternatively, if your version does not have the class methods shown above:

// TBytesStream was added in D2009, so define it manually for D2007

uses
  ..., IdCoder, IdCoderMIME
  {$IF RTLVersion < 20)
  , RTLConsts
  {$IFEND}
  ;

{$IF RTLVersion < 20)
type
  TBytesStream = class(TMemoryStream)
  private
    FBytes: TBytes;
  protected
    function Realloc(var NewCapacity: Longint): Pointer; override;
  public
    constructor Create(const ABytes: TBytes); overload;
    property Bytes: TBytes read FBytes;
  end;

constructor TBytesStream.Create(const ABytes: TBytes);
begin
  inherited Create;
  FBytes := ABytes;
  SetPointer(Pointer(FBytes), Length(FBytes));
  FCapacity := FSize;
end;

const
  MemoryDelta = $2000; // Must be a power of 2

function TBytesStream.Realloc(var NewCapacity: Integer): Pointer;
begin
  if (NewCapacity > 0) and (NewCapacity <> FSize) then
    NewCapacity := (NewCapacity + (MemoryDelta - 1)) and not (MemoryDelta - 1);
  Result := Pointer(FBytes);
  if NewCapacity <> FCapacity then
  begin
    SetLength(FBytes, NewCapacity);
    Result := Pointer(FBytes);
    if NewCapacity = 0 then
      Exit;
    if Result = nil then raise EStreamError.CreateRes(@SMemoryStreamError);
  end;
end;
{$IFEND}

var
  Bytes: TBytes;
  BStrm: TBytesStream;
  Encoder: TIdEncoderMIME;
  Decoder: TIdDecoderMIME;
  Base64String: String;
begin
  //...
  Bytes := ...; // array of bytes
  //...
  BStrm := TBytesStream.Create(Bytes);
  try
    Encoder := TIdEncoderMIME.Create;
    try
      Base64String := Encoder.Encode(BStrm);
    finally
      Encoder.Free;
    end;
  finally
    BStrm.Free;
  end;
  //...
  BStrm := TBytesStream.Create;
  try
    Decoder := TIdDecoderMIME.Create;
    try
      Decoder.DecodeBegin(BStrm);
      Decoder.Decode(Base64String);
      Decoder.DecodeEnd;
    finally
      Decoder.Free;
    end;
    Bytes := BStrm.Bytes;
  finally
    BStrm.Free;
  end;
  //...
end;

encoding, i have encode array of bytes base64 string (and decode string) on old delphi 2007. how do? further informations: i've tried synapse (as� I have to encode an array of bytes to a base64 string (and decode this string) on an old Delphi 2007. How could I do? Further Informations: I've tried synapse (As suggested here Binary to Base64

Contrary to what you state in the question, the EncdDecd unit is included in Delphi 2007. You can simply use that.

Delphi 7 - Decode base 64 string to a file - delphi - android, Try using a base64 decoder which supports decoding to a stream, and were using a pre-Unicode version of Delphi (2007 and earlier) running your app on a� Encode base64 and Decode base64 using delphi 2007 Tag: delphi , encoding , base64 , decoding , delphi-2007 I have to encode an array of bytes to a base64 string (and decode this string) on an old Delphi 2007.

David Heffernan responded very well!

Add in your uses the class "EncdDecd", it will have the procedures:

function DecodeString (const Input: string): string;
function DecodeBase64 (const Input: string): TBytes;

Testing with https://www.base64encode.org/

The String "Working" in both Delphi and the site resulted in: "V29ya2luZw =="

ShowMessage ('Working =' + EncodeString ('Working'));
ShowMessage ('Working =' + DecodeString ('V29ya2luZw =='));

SwissDelphiCenter.ch : encode / decode Base64 string?, All about Borland Delphi. Programming tips, downloads, forums, news, topsites, newsletter. Convert a base64 encoded text into an decoded string or download as a binary file using this free online base64 decoder utility. Base64 to text. Base64 to Encode base64 and Decode base64 using delphi 2007. I have to encode an array of . https://www.convertstring.com/EncodeDecode/Base64Decode. TChilkatBinData Delphi ActiveX Reference Documentation

Here goes EncodeToBase64:

uses
classes, sysutils;

function EncodeToBase64(var Buffer: TBytes): Longint;
const
  EncodingTable: PChar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
var
  WriteBuf: array[0..3] of Byte;
  Buf: array[0..2] of Byte;
  Dest: TMemoryStream;
  i, j, Count: Integer;
begin
  Result := 0;

  Count := Length(Buffer);
  j := Count div 3;

  if j > 0 then
  begin
    Dest:= TMemoryStream.Create();  
    try
      Dest.Position := 0;
      for i := 0 to j - 1  do
      begin
        Move(Buffer[i * 3], Buf[0], 3); 
        WriteBuf[0] := Ord(EncodingTable[Buf[0] shr 2]);
        WriteBuf[1] := Ord(EncodingTable[(Buf[0] and 3) shl 4 or (Buf[1] shr 4)]);
        WriteBuf[2] := Ord(EncodingTable[(Buf[1] and 15) shl 2 or (Buf[2] shr 6)]);
        WriteBuf[3] := Ord(EncodingTable[Buf[2] and 63]);  
        Dest.Write(WriteBuf, 4);  
        Inc(Result, 4);
        Dec(Count, 3);    
      end;          

      if Count in [1, 2] then  
      begin           
        Move(Buffer[i * 3], Buf[0], Count);
        WriteBuf[0] := Ord(EncodingTable[Buf[0] shr 2]);
        WriteBuf[1] := Ord(EncodingTable[(Buf[0] and 3) shl 4 or (Buf[1] shr 4)]); 
        if Count = 1 then
          WriteBuf[2] := Ord('=')
        else                                           
          WriteBuf[2] := Ord(EncodingTable[(Buf[1] and 15) shl 2 or (Buf[2] shr 6)]);
        WriteBuf[3] := Ord('='); 
        Dest.Write(WriteBuf, 4);  
        Inc(Result, 4);
        Dec(Count, Count);
      end;

      if Result > 0 then
      begin
        SetLength(Buffer, Result);
        Dest.Position := 0;
        Dest.Read(Buffer[0], Result);   
      end;  
    finally
      Dest.Free;
    end;
  end;

end; 

And this should work without any special units or components.

getting base64 encoded string using IdHttp, You should decode the stream first. Encoding image to base64 string using javascript? The most common problem getting Delphi 7-Delphi 2007 components to compile seems to be caused by the new Unicode strings in� I have to encode an array of bytes to a base64 string (and decode this string) on an old Delphi 2007. How could I do? Further Informations: I've tried synapse (As suggested here Binary to Base64 (Delphi)).Indy ships with Delphi, and has TIdEncoderMIM

Hidden Gems: Base64 Encoding and Decoding in Delphi , Just today I was pondering how to encode into or decode from Base64 with Delphi. As it turns out, Embarcadero included the functionality in the� The problem is not necessarily in your code to decode. The problem is that your decoding does not match the process that originally encoded. The fault could lie either in the original encoding, or the decoding. You need to check how the base64 value was encoded. Was it encoded using the steps described above?

Delphi Decode Base64 � talsbi.datesweetcherrygirls.xyz, I used Binary to Base64 function that you answered: Binary to Base64 Delphi Base64 encode your data in a hassle-free way, or decode it into human-readable format. Hello, I have a Unit where in Delphi 2007 i was able to encode/decode � How to encode file of any type into base64 string and then decode it into file again using Lazarus/Delphi? -1 Convert a “TMemoryStream” to “String” and vice-versa

Base64 Encoding/Decoding function for Delphi, Base64Encoding function is frequently used in MIME Type based programming. Here's the good resource for supporting base64encode and� The problem is not necessarily in your code to decode. The problem is that your decoding does not match the process that originally encoded. The fault could lie either in the original encoding, or the decoding. You need to check how the base64 value was encoded. Was it encoded using the steps described above?

Comments
  • Base64 is a standard encoding regardless of language or version. Which means there's something wrong with either how it's being encoded or decoded. Can we see your code for both?
  • You can use Indy try the TIdDecoderMIME class.
  • And TIdEncoderMIME
  • The EncdDecd.pas is not in $(DELPHI)\Source\Internet path?
  • @Jerry Dodge: You're right, trying with a small example, synapse lib functions seem to work as expected. There should be something wrong in my code (Or a particular case), I'll check this.
  • It works good on further delphi version! Thanks for your suggestion and sample, I'll check this on Delphi 2007 as soon as possible, but I suppose it also works on old ide.
  • @Hwau: you can upgrade Indy to the latest version, even on older Delphi's
  • Unfortunately, EncodeBytes and DecodeBytes functions are undeclared using Delphi2007. I would prefer to don't upgrade indy manually, is there another way?
  • @Hwau: You will have to look in D2007's IdCoder unit and see what functions are actually available in that version. Worse case, the class methods might not be available, so try instantiating TIdEncoderMIME/TIdDecoderMIME objects and use their non-class methods instead. I updated my answer with an example.
  • @RemyLebeau: I'm sorry for long silence, TBytesStream seems to be undefined in Delphi-2007, how else could I load bytes array into stream?