Decoding base64-encoded string through FROM_BASE64 () function in MySQL
I don't know if you have any knowledge about the similar articles in MySQL that use the FROM_BASE64 () function to decode base64-encoded strings. Today, I'm here to give you a brief introduction. If you are interested, let's take a look at the body. I believe you will get something after reading the FROM_BASE64 () function to decode the base64-encoded string in MySQL.
In MySQL, the FROM_BASE64 () function decodes a base-64-encoded string and returns the result. More specifically, it accepts a string encoded with the base-64 encoding rules used by TO_BASE64 () and returns the decoded result in the form of a binary string.
The syntax of FROM_BASE64 () is as follows:
FROM_BASE64 (str)
Where the parameter str is the base-64-encoded string you want to decode.
Example 1-basic usage
Here is an example to demonstrate the basic usage:
SELECT FROM_BASE64 ('Q2F0')
Results:
+-+ | FROM_BASE64 ('Q2F0') | +-+ | Cat | +-+
In this example, our parameter is Q2F0, which is the base-64-encoded string of Cat.
We can get the base-64-encoded string by passing Cat to the TO_BASE64 () function:
SELECT TO_BASE64 ('Cat')
Results:
+-+ | TO_BASE64 ('Cat') | +-+ | Q2F0 | +-+
Example 2-A long string
Here is an example of using a longer string:
SELECT FROM_BASE64 ('TXkgY2F0IGxpa2VzIHRvIGNoYXNlIGVsZXBoYW50cyE=')
Results:
+-+ | FROM_BASE64 ('TXkgY2F0IGxpa2VzIHRvIGNoYXNlIGVsZXBoYW50cyE=') | + -+ | My cat likes to chase elephants! | +-- +
Example 3-invalid parameter
If the parameter is not a valid base-64 string, NULL is returned:
SELECT FROM_BASE64 ('Oopsfolk')
Results:
+-- + | FROM_BASE64 ('Oopsfolk') | +-- + | NULL | +-- +
Example 4-NULL parameters
If you pass in NULL, you will also get NULL:
SELECT FROM_BASE64 (NULL)
Results:
+-+ | FROM_BASE64 (NULL) | +-+ | NULL | +-+
Example 5-missing parameter
If you don't pass a parameter, you'll get an error:
SELECT FROM_BASE64 ()
Results:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'FROM_BASE64'
Example 6-too many parameters
If you pass in too many parameters, you will also get an error:
SELECT FROM_BASE64 ('Q2F0,' RWxlcGhhbnQ=')
Results:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'FROM_BASE64'
After reading the article in MySQL that decodes base64-encoded strings through the FROM_BASE64 () function, what do you think? If you want to know more about it, you can continue to follow our industry information section.