MySQL FROM_BASE64() Function

In MySQL, the FROM_BASE64() function decodes the base-64 encoded data and returns the result as a binary string.

The FROM_BASE64() function can decode the result of the TO_BASE64() function.

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.

FROM_BASE64() Syntax

Here is the syntax of MySQL FROM_BASE64() function:

FROM_BASE64(str)

Parameters

str
Required. The string used for base-64 decoding .

Return value

The FROM_BASE64(str) function decodes the encoded data and returns the result as a binary string.

The function will return NULL if str parameter is NULL.

Since the FROM_BASE64() function returns a binary string, the result displayed on the mysql client may be hexadecimal. Please use --binary-as-hex option to disable displaying binary content as hex.

FROM_BASE64() Examples

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

If we didn’t disable --binary-as-hex, the output will be:

             TO_BASE64('Hello World'): SGVsbG8gV29ybGQ=
FROM_BASE64(TO_BASE64('Hello World')): 0x48656C6C6F20576F726C64
                    FROM_BASE64(NULL): NULL