Base64 Encoding in Erlang
Our example demonstrates base64 encoding and decoding in Erlang. Erlang provides built-in support for base64 encoding/decoding through the base64
module.
In Erlang, we use the base64
module for encoding and decoding. The encode/1
function is used for standard base64 encoding, while encode_to_string/1
is used for URL-compatible encoding. Both functions return a string.
For decoding, we use the decode/1
function, which returns a tuple {ok, DecodedBinary}
on success or {error, Reason}
on failure. We’re using pattern matching to extract the decoded binary.
To run the program, save it as base64_encoding.erl
and use the Erlang shell:
The string encodes to the same value with both the standard and URL-compatible base64 encoders in Erlang, and they both decode to the original string as desired.
Note that in Erlang, strings are represented as lists of integers, and the base64 functions work with binaries. The conversion between these types is handled automatically in this example for simplicity.