Hidden Gems: Base64 Encoding and Decoding in Delphi

One of the big probs with Delphi these days is the sheer amount of functionality that is already included in the package. Just today I was pondering how to encode into or decode from Base64 with Delphi. 

As it turns out, Embarcadero included the functionality in the SOAP source files that can be found in the “soap” directory.

One unit in particular, called Soap.EncdDecd , contains the functions that I was looking for. However, the comments in the source file pointed me yet in another direction:

You find the TNetCoding class in the unit System.NetEncoding :

The class methods Base64, HTML and URL allow us to encode strings, byte arrays and streams. To Base64 encode a string, we simply need to state:

Overloading makes it very easy as the call is the very same for steams and byte arrays.

A simple demo with 3 TEdit  components shows the functionality. The text entered in the first edit is displayed encoded in the second. The third edit displays the text from the second after decoding.

Considering that we needed 3rd party component libraries for these things before, shows how significantly Delphi has grown. The biggest problem nowadays is to find the right class for the job and keep track of the new innovations available to us.

base64demo

 

Posted in Delphi Tagged with: , , , , ,