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 old Delphi versions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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; |