MySQL TO_BASE64() Function

In MySQL, the TO_BASE64() function converts the string argument to base-64 encoded form and returns the string representation.

Unlike existing base-64 schemes , MySQL FROM_BASE64() and TO_BASE64() functions have some special rules:

  • The encoding for alphabet value 62 is '+'.
  • The encoding for alphabet value 63 is '/'.
  • The encoded output consists of 4 printable characters. Every 3 bytes of input data is encoded using 4 characters. If the last group is incomplete, it is padded with ‘=’ characters to a length of 4.
  • Adds a newline after every 76 characters of encoded output to split long output into multiple lines.
  • Decoding recognizes and ignores newlines, carriage returns, tabs, and spaces.

TO_BASE64() Syntax

Here is the syntax of MySQL TO_BASE64() function:

TO_BASE64(str)

Parameters

str
Required. The string to be encoded.

Return value

The TO_BASE64(str) function encodes the given string as base-64 form and returns the string representation.

The function will return NULL if str parameter is NULL.

TO_BASE64() Examples

SELECT
    TO_BASE64('a'),
    TO_BASE64('Hello World'),
    FROM_BASE64(TO_BASE64('Hello World')),
    TO_BASE64(NULL)\G
                       TO_BASE64('a'): YQ==
             TO_BASE64('Hello World'): SGVsbG8gV29ybGQ=
FROM_BASE64(TO_BASE64('Hello World')): Hello World
                      TO_BASE64(NULL): NULL

Note that if you are using the mysql client, the FROM_BASE64() function results are displayed in hexadecimal. Please use --binary-as-hex option to disable displaying binary content as hex.