Base64 Encoding in Wolfram Language

Our first example demonstrates base64 encoding and decoding in Wolfram Language. Here’s the full source code:

(* Import the necessary functions *)
Needs["GeneralUtilities`"]

(* Define the string we'll encode/decode *)
data = "abc123!?$*&()'-=@~";

(* Encode using standard base64 *)
sEnc = Base64Encode[data];
Print[sEnc]

(* Decode the standard base64 string *)
sDec = Base64Decode[sEnc];
Print[sDec]
Print[]

(* Encode using URL-safe base64 *)
uEnc = URLEncode[data];
Print[uEnc]

(* Decode the URL-safe base64 string *)
uDec = URLDecode[uEnc];
Print[uDec]

In Wolfram Language, we use the Base64Encode and Base64Decode functions for standard base64 encoding and decoding. For URL-safe encoding and decoding, we use URLEncode and URLDecode.

To run this code, you can copy it into a Wolfram Language notebook or a .wl file and evaluate it.

YWJjMTIzIT8kKiYoKSctPUB+
abc123!?$*&()'-=@~

abc123%21%3F%24%2A%26%28%29%27-%3D%40%7E
abc123!?$*&()'-=@~

The output shows that both standard base64 and URL-safe encoding successfully encode and decode the original string. Note that the URL-safe encoding uses percent encoding for special characters, which is different from the base64 URL-safe encoding in some other languages, but serves a similar purpose of making the encoded string safe for use in URLs.

In Wolfram Language, the distinction between standard and URL-safe base64 encoding is not as explicit as in some other languages. The URLEncode function provides a way to encode strings for safe use in URLs, which covers the use case of URL-safe base64 encoding in many scenarios.