Skip to content

Core\Encoding::toBase64

Writes bytes as base64 text, so that they can travel where only text may go.

Base64 writes any bytes using the letters A to Z in both cases, the digits 0 to 9, and the two symbols + and /. Every three bytes become four characters, so the text is a third longer than the buffer. When the last group is short, = is written to fill it. Every byte has a spelling, so this member never fails, and the empty buffer gives the empty string.

Programs use this form wherever only text may travel: an HTTP header, an email attachment, a small image inside a stylesheet, a field in a JSON document.

Good to know: +, / and = all have their own meaning inside a URL. For a URL or a JSON Web Token use Core\Encoding::toBase64Url, which writes - and _ in their place and no padding at all.

Parameters

  • $bbytesThe bytes to encode.

Return value

Returns string. The base64 text. Empty bytes give the empty string.

Examples

Bytes out and bytes back

<?nvs
// Shows the round trip and the padding: bytes go out as text, and the same
// bytes come back.
bytes $raw = Core\Encoding::encodeText('hello', Core\Charset::Utf8);
// Five bytes become eight characters. This prints aGVsbG8=.
echo Core\Encoding::toBase64($raw), "\n";
// `Core\Encoding::fromBase64` reads the text back. This prints hello.
bytes $back = Core\Encoding::fromBase64(Core\Encoding::toBase64($raw));
echo Core\Encoding::decodeText($back, Core\Charset::Utf8), "\n";
// Three bytes fill a group exactly, so nothing is added. This prints YWJj.
echo Core\Encoding::toBase64(Core\Encoding::encodeText('abc', Core\Charset::Utf8)), "\n";
// One byte leaves most of a group empty, and `=` fills it. This prints YQ==.
echo Core\Encoding::toBase64(Core\Encoding::encodeText('a', Core\Charset::Utf8)), "\n";
// The empty buffer gives the empty string, so this line prints nothing.
echo Core\Encoding::toBase64(Core\Encoding::encodeText('', Core\Charset::Utf8)), "\n";
Output
aGVsbG8=
hello
YWJj
YQ==

How much longer the text is

<?nvs
// Shows how much longer the text is than the bytes, which matters wherever
// something has a size limit.
// Every three bytes become four characters, and `=` fills a short last group.
// So the length always divides by four. This prints 1 4, 2 4, 3 4, 4 8 and 5 8.
uint $size = 1;
while ($size <= 5) {
echo $size, ' ', Core\Str::length(Core\Encoding::toBase64(Core\Bytes::fill($size, 65))), "\n";
$size = $size + 1;
}
// Over a whole buffer that works out a third longer. A header or a database
// column with a limit has to allow for it. This prints 300 400.
bytes $payload = Core\Bytes::fill(300, 65);
echo Core\Bytes::length($payload), ' ', Core\Str::length(Core\Encoding::toBase64($payload)), "\n";
// The alphabet holds two symbols as well as letters and digits. These two
// bytes need both of them. This prints +/8=.
echo Core\Encoding::toBase64(Core\Encoding::fromHex('fbff')), "\n";
Output
1 4
2 4
3 4
4 8
5 8
300 400
+/8=

Bytes inside a json document

<?nvs
// Shows the everyday use: bytes are sent inside a JSON document, which
// carries text and never bytes.
bytes $thumbnail = Core\Encoding::fromHex('89504e470d0a1a0a');
// The bytes become a field the document can hold. This prints
// {"name":"logo.png","data":"iVBORw0KGgo="}.
array<string> $document = [
'name' => 'logo.png',
'data' => Core\Encoding::toBase64($thumbnail),
];
echo Core\Json::encode($document), "\n";
// The other side reads the field back into the same bytes. `Core\Bytes::compare`
// gives 0 for two buffers that are the same, so this prints 8 0.
bytes $received = Core\Encoding::fromBase64($document['data']);
echo Core\Bytes::length($received), ' ', Core\Bytes::compare($received, $thumbnail), "\n";
Output
{"name":"logo.png","data":"iVBORw0KGgo="}
8 0