5

OK, well this is driving me nuts, lol.

I have a Base64 string and am trying to decode it into a TMemoryStream using TIdDecoderMIME.

My current code is as follows:

Var MStream:TMemoryStream; Decoder:TIdDecoderMIME;
begin
  Decoder := TIdDecoderMIME.Create(nil);
  MStream := TMemoryStream.Create;
  Decoder.DecodeToStream(BSting,MStream);
end;

Where BString = My Base64 string.

Now when the code is ran, I get an error message saying "Uneven size in DecodeToString."

Any ideas?

Any help is greatly appreciated. Thanks.

TLama
  • 75,147
  • 17
  • 214
  • 392
Josh Line
  • 625
  • 3
  • 13
  • 27

2 Answers2

4

You're passing to the DecodeToStream function a Base64 string whose length is not a multiple of 4. In other words, the string you're passing is invalid.

TLama
  • 75,147
  • 17
  • 214
  • 392
1

Base64 strings are normally padded with trailing "=" signs to make sure their length is a multiple of 4.

Some decoders will try to correct for the missing padding chars while others will not. See the StackOverflow question "Remove trailing “=” when base64 encoding"

The TIdDecoderMime object validates the input by making sure it is a multiple of 4 - which it will be if the padding chars are included in the input.

Community
  • 1
  • 1
Mark Elder
  • 3,987
  • 1
  • 31
  • 47