Κυριακή 13 Μαΐου 2012

A general purpose Base64 decoding routine using Indy

This little routine is for use with Indy Component (v10) to decode some base-64 encoded information.Choose TBytes return type because base-64 encoding can be used for any sequence of "octets". You better to not return a string since that implies text was encoded. We can easily return a string from the byte array.
As well as TBytes we use the relatively new TBytesStream stream class, so you will need to make some changes if using older Delphis. 


function Base64Decode(const EncodedText: string): TBytes;
var
  DecodedStm: TBytesStream;
  Decoder: TIdDecoderMIME;
begin
  Decoder := TIdDecoderMIME.Create(nil);
  try
    DecodedStm := TBytesStream.Create;
    try
      Decoder.DecodeBegin(DecodedStm);
      Decoder.Decode(EncodedText);
      Decoder.DecodeEnd;
      Result := DecodedStm.Bytes;
    finally
      DecodedStm.Free;
    end;
  finally
    Decoder.Free;
  end;
end;

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου