w3resource

MySQL CONV() function

CONV() function

MySQL CONV() converts a number from one numeric base number system to another numeric base number system. After the conversion, the function returns a string representation of the number.

When the argument defined is a NULL, the return value will be NULL.

The minimum base is 2 and the maximum base is 36. If the base to be converted to is a negative number, the number is regarded as a signed number. Otherwise, it is treated as unsigned.

This function is useful in -

  • CONV() is a powerful function to convert numbers from one numeric base to another, for instance, from binary to decimal, or from hexadecimal to octal.
  • Different numeric bases are used in cryptography and data compression algorithms to increase efficiency and security that managed by CONV().
  • In electrical engineering and digital electronics, CONV() is used to convert numbers between binary, octal, decimal, and hexadecimal bases which are common in these fields.
  • In web development, when generating unique IDs or shortening URLs, CONV() can be used to convert database-generated IDs to a more compact representation.
  • In software development, when debugging or testing code that involves different bases (e.g., bit manipulation), CONV() can help verify the correctness of operations.
  • In low-level programming, like assembly language, CONV() can be used to convert memory addresses from hexadecimal to binary for operations on specific bits.

Syntax:

CONV(num , from_base , to_base );

Arguments:

Name Description
num A number.
from_base Existing base of the number num.
to_base Base of the number num after conversion.

Syntax Diagram:

MySQL CONV() Function - Syntax Diagram

MySQL Version: 8.0


Pictorial presentation of MySQL CONV() function

pictorial presentation of MySQL CONV() function

Example of MySQL CONV() function

Code:

SELECT CONV(15,10,2);

Explanation:

The above MySQL statement will convert the numeric value of 15 from decimal number system to binary number system.

Output:

mysql> SELECT CONV(15,10,2);
+---------------+
| CONV(15,10,2) |
+---------------+
| 1111          | 
+---------------+
1 row in set (0.02 sec)

Example : CONV() function using character value

Code:

SELECT CONV('b',16,10)'Hexadecimal to Decimal',
  CONV('b',16,2) AS 'Hexadecimal to Binary';

Explanation:

The above MySQL statement will convert the hexadecimal ‘b’ in decimal number and binary number.

Output:

mysql> SELECT CONV('b',16,10)'Hexadecimal to Decimal',
    ->   CONV('b',16,2) AS 'Hexadecimal to Binary';
+------------------------+-----------------------+
| Hexadecimal to Decimal | Hexadecimal to Binary |
+------------------------+-----------------------+
| 11                     | 1011                  | 
+------------------------+-----------------------+
1 row in set (0.02 sec)

Example : CONV() function using negative base

Code:

SELECT CONV(19,10,-16);

Explanation:

The above MySQL statement will convert a decimal value 19 to a hexadecimal number. Here the base to be converted is -16, so it is treated as an unsigned number.

Output:

mysql> SELECT CONV(19,10,-16);
+-----------------+
| CONV(19,10,-16) |
+-----------------+
| 13              | 
+-----------------+
1 row in set (0.00 sec)

All Mathematical Functions

Previous:CEILING()
Next: COS()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mysql/mathematical-functions/mysql-conv-function.php