View source code
Display the source code in std/base64.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Template std.base64.Base64Impl

Template for implementing Base64 encoding and decoding.

template Base64Impl(char Map62th, char Map63th, char Padding = '=') ;

For most purposes, direct usage of this template is not necessary; instead, this module provides default implementations: Base64, implementing basic Base64 encoding, and Base64URL and Base64URLNoPadding, that implement the Base64 variant for use in URLs and filenames, with and without padding, respectively.

Customized Base64 encoding schemes can be implemented by instantiating this template with the appropriate arguments. For example:

// Non-standard Base64 format for embedding in regular expressions.
alias Base64Re = Base64Impl!('!', '=', Base64.NoPadding);

Contained Functions

decode Decodes source into the given buffer.
decode Decodes source into a given output range.
decode Decodes source into newly-allocated buffer.
decodeLength Given a Base64 encoded string, calculates the length of the decoded string.
decoder Construct a Decoder that iterates over the decoding of the given Base64 encoded data.
encode Encode source into a char[] buffer using Base64 encoding.
encode Encodes source into an output range using Base64 encoding.
encode Encodes source to newly-allocated buffer.
encodeLength Calculates the length needed to store the encoded string corresponding to an input of the given length.
encoder Construct an Encoder that iterates over the Base64 encoding of the given input range.

Contained Structs

Decoder An input range that iterates over the decoded data of a range of Base64 encodings.
Decoder An input range that iterates over the bytes of data decoded from a Base64 encoded string.
Encoder An input range that iterates over the respective Base64 encodings of a range of data items.
Encoder An input range that iterates over the encoded bytes of the given source data.


Encoded strings will not have any padding if the Padding parameter is set to NoPadding.


import std.string : representation;

// pre-defined: alias Base64 = Base64Impl!('+', '/');
ubyte[] emptyArr;
writeln(Base64.encode(emptyArr)); // ""
writeln(Base64.encode("f".representation)); // "Zg=="
writeln(Base64.encode("foo".representation)); // "Zm9v"

alias Base64Re = Base64Impl!('!', '=', Base64.NoPadding);
writeln(Base64Re.encode("f".representation)); // "Zg"
writeln(Base64Re.encode("foo".representation)); // "Zm9v"


Masahiro Nakagawa, Daniel Murphy (Single value Encoder and Decoder)


Boost License 1.0.