Search code examples
delphibase64decodedelphi-xe7utf8-decode

Base64-decode a string using NetEncoding in Delphi XE7?


In Delphi XE7 Update 1, when trying to execute this code in a VCL program:

uses System.NetEncoding;
...
tempstring := TNetEncoding.Base64.Decode(tempstring);

I get this error message:

Error message

So how can I base64-decode a string using NetEncoding?


Solution

  • You are calling the Decode overload that accepts base64 encoded text and returns a string. The way that Decode method is implemented is as follows:

    1. Decode the base64 to binary.
    2. Treat the binary as UTF-8 encoded text.
    3. Decode the UTF-8 bytes to text.

    Your error message indicates that the binary that results from step 1 is not valid UTF-8. In order to work out why this is so requires knowledge of where the base64 encoded text originated.

    Your current code assumes that the following process took place to create the base64 encoded value:

    1. Encode some text to UTF-8 bytes.
    2. Base 64 encode those bytes.

    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?

    If I had to guess, I would suspect that the original data was not in fact textual. My guess is that the original data is binary, that is a stream of bytes. I suspect that you started from some code which held a binary data in an AnsiString variable. It is very common to see Delphi code that abuses strings in this way. For historical reasons it seems to have been written in to the lore that AnsiString can be used to hold binary data. I guess this all started when Delphi 2 introduced AnsiString and at that time there were no dynamic arrays.

    Anyway, if you have previously been using AnsiString to hold binary data then you should take this opportunity to stop doing so. Use TBytes, for instance, for that purpose. In which case you would use:

    var
      bytes: TBytes;
    ....
    bytes := TNetEncoding.Base64.DecodeStringToBytes(...);
    

    to decode your base64 value to binary.

    So, suppose that the base64 that you have is actually ANSI encoded text. Then you would decode it like this:

    var
      bytes: TBytes;
      text: string;
    ....
    bytes := TNetEncoding.Base64.DecodeStringToBytes(...);
    text := TEncoding.ANSI.GetString(bytes);
    

    This assumes the prevailing ANSI code page on the machine on which you decode. If you need to specify an ANSI code page then you might write it like this:

    var
      bytes: TBytes;
      text: string;
      Encoding: TEncoding;
    ....
    bytes := TNetEncoding.Base64.DecodeStringToBytes(...);
    Encoding := TEncoding.GetEncoding(CodePage);
    try
      text := Encoding.GetString(bytes);
    finally
      Encoding.Free;
    end;
    

    As you can see, I am now reduced to guessing. If you want to make progress you will find out how your data was encoded.