EncodeString

This week I had to migrate an application from Delphi XE3 to Delphi Berlin and this application uses a Base64 encoding.

Normally Base64 encoding is not difficult stuff and Delphi XE3 simply offers the function EncodeString in the unit Soap.EncdDecd.

Since Delphi Berlin has the same function I thought that I didn’t have to change a single line of code. But the first thing I realized is that the function is nearly deprecated. This means that it already has a deprecated comment: // deprecated ‘Use TNetEncoding.Base64.Encode’.

Such a comment means that the function will become deprecated with one of the next Delphi releases.

A short look at the implementation shows the reason:

I should call TNetEncoding.Base64.Encode directly instead of using the old function EncodeString.

But then I realized that the two Delphi versions show different results.

Let’s have a look at a small example with some German umlauts:

Delphi XE3 shows ‘xOTW9tz83w==’ whereas Delphi Berlin shows ‘w4TDpMOWw7bDnMO8w58=’.

So bad luck, I had to have a deeper look at the code. Let’s start with Delphi XE3:

This means that the string is first converted into bytes with the help of the default encoding which is under Windows an ANSI encoding. After that the bytes are encoded.

Let’s have a look at the Delphi Berlin implementation:

Delphi Berlin always converts the string into UTF-8 bytes and then encodes the bytes. This means that the implementation of EncodeString in Delphi Berlin differs to the implementation in XE3.

To fix this issue let’s try the following implementation in Delphi Berlin:

I first convert the string into bytes with the help of the default encoding. Then I encode the bytes with the new Base64 class and at the end I have to convert the bytes back to a string.

I will contact Embarcadero/Idera and ask them if I should write a quality portal case.

This entry was posted in C++-Builder, Delphi, FireMonkey, RAD Studio 10 Seattle, RAD Studio 10.1 Berlin, RAD Studio XE5, RAD Studio XE6, RAD Studio XE7, RAD Studio XE8, Tips and Tricks, Uncategorized. Bookmark the permalink.

4 Responses to EncodeString

  1. Stefan Glienke says:

    IMO the old behavior was wrong/broken and the new one is correct.

    • roman says:

      Stefan,

      you are absolutely right. I think the same and told Marco about it.

      • Stefan Glienke says:

        I am confused. XE3 was broken, Berlin (or anywhere since XE4) is fixed. What is left to tell them? UTF8 is the way to go any not some codepage specific one.

        • roman says:

          What should I say? I found out that there is a difference between XE3 and Berlin, fixed it and chated with Marco and you about it.
          Then we all came to the conclusion that the Berlin version is correct.

          But anyway people now know about the issue. Maybe someone else wants to move from XE to Berlin and has the same issue.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.