How the CONV() function works in Mariadb?
The CONV()
function is a mathematical function that converts a number from one base to another base. The function returns a string that represents the converted number in the new base.
The CONV()
function is a mathematical function that converts a number from one base to another base. The function returns a string that represents the converted number in the new base. The function can handle numbers in binary, octal, decimal, and hexadecimal bases.
Syntax
The syntax of the CONV()
function is as follows:
CONV(number, from_base, to_base)
The function takes three arguments, number
, from_base
, and to_base
, which are the number to be converted, the base of the original number, and the base of the converted number, respectively. The arguments can be integers or strings that represent integers. The base arguments must be between 2 and 36, inclusive. The function returns NULL if any argument is NULL or invalid.
Examples
Example 1: Converting a decimal number to binary
The following example uses the CONV()
function to convert a decimal number to binary.
SELECT CONV(42, 10, 2);
The output is:
+-----------------+
| CONV(42, 10, 2) |
+-----------------+
| 101010 |
+-----------------+
The output shows that the decimal number 42 is equivalent to the binary number 101010.
Example 2: Converting a hexadecimal number to octal
The following example uses the CONV()
function to convert a hexadecimal number to octal.
SELECT CONV('FF', 16, 8);
The output is:
+-------------------+
| CONV('FF', 16, 8) |
+-------------------+
| 377 |
+-------------------+
The output shows that the hexadecimal number FF is equivalent to the octal number 377.
Example 3: Converting a binary number to decimal
The following example uses the CONV()
function to convert a binary number to decimal.
SELECT CONV('1101', 2, 10);
The output is:
+---------------------+
| CONV('1101', 2, 10) |
+---------------------+
| 13 |
+---------------------+
The output shows that the binary number 1101 is equivalent to the decimal number 13.
Example 4: Converting a number with a negative base
The following example uses the CONV()
function to convert a number with a negative base. The function can handle negative bases by using a minus sign before the base argument.
SELECT CONV(15, -10, -2);
The output is:
+-------------------+
| CONV(15, -10, -2) |
+-------------------+
| 1111 |
+-------------------+
The output shows that the number 15 in base -10 is equivalent to the number 1111 in base -2.
Example 5: Converting a number with an invalid base
The following example uses the CONV()
function to convert a number with an invalid base. The function returns NULL if the base argument is not between 2 and 36, inclusive.
SELECT CONV(42, 10, 37);
The output is:
+------------------+
| CONV(42, 10, 37) |
+------------------+
| NULL |
+------------------+
The output shows that the function returns NULL, as expected.
Related Functions
There are some other functions that are related to the CONV()
function in Mariadb. They are:
BIN()
: This function converts a decimal number to a binary string.OCT()
: This function converts a decimal number to an octal string.HEX()
: This function converts a decimal number or a string to a hexadecimal string.UNHEX()
: This function converts a hexadecimal string to a binary string.
Conclusion
The CONV()
function is a useful function to convert a number from one base to another base. It can handle numbers in binary, octal, decimal, and hexadecimal bases, as well as negative bases. It returns a string that represents the converted number in the new base. It is similar to the BIN()
, OCT()
, HEX()
, and UNHEX()
functions, but with more flexibility. It is also related to some other functions that perform mathematical operations on numbers.