MariaDB FROM_BASE64() Function

In MariaDB, FROM_BASE64() is a built-in string function that decodes a given base-64 encoded string and returns the result as a binary string.

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

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

  • The encoding for the letter value 62 is '+'.
  • The encoding for the letter value 63 is '/'.
  • Encoded output consists of 4 printable characters. Every 3 bytes of input data are encoded using 4 characters. If the length of the last group is less than 4, it will be filled with '=' characters.
  • Adds a newline after every 76 characters of encoded output to break long output across multiple lines.
  • Decoding recognizes and ignores newlines, carriage returns, tabs, and spaces.

MariaDB FROM_BASE64() Syntax

Here is the syntax of the MariaDB FROM_BASE64() function:

FROM_BASE64(str)

Parameters

str

Required. An base-64 encoded string.

If you do not provide a parameter, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'FROM_BASE64'.

Return value

The MariaDB FROM_BASE64(str) function decodes base-64 encoded data and return the result as a binary string.

The FROM_BASE64() function returns NULL when the argument str is NULL.

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

MariaDB FROM_BASE64() Examples

Basic example

The following example shows the basic usage of the MariaDB FROM_BASE64() function.

SELECT
  TO_BASE64('Hello'),
  FROM_BASE64(TO_BASE64('Hello'));

Output:

+--------------------+---------------------------------+
| TO_BASE64('Hello') | FROM_BASE64(TO_BASE64('Hello')) |
+--------------------+---------------------------------+
| SGVsbG8=           | Hello                           |
+--------------------+---------------------------------+

In this example, we encoded Hello using the TO_BASE64() function.

Null values

The FROM_BASE64() function returns NULL when the argument str is NULL.

SELECT FROM_BASE64(null);

Output:

+-------------------+
| FROM_BASE64(null) |
+-------------------+
| NULL              |
+-------------------+

Conclusion

The MariaDB FROM_BASE64() function decodes base-64 encoded data and return the result as a binary string.