The CONV SQL Function in MySQL and MariaDB - Convert numbers |
|
| CONV | Syntax: | CONV(Value, from Base, to Base) | Return value: | CHAR | Function type: | Conversion function | |
| | The CONV() function converts numbers between different number systems.
CONV() returns a string of "Value" converted "from Base" "to Base".
If "Value" is NULL, or if "from Base" or "to Base" are invalid, CONV() returns NULL back.
The minimum base is 2 and the maximum base is 36.
Similar fixed base functions are: BIN(), OCT(), HEX() and UNHEX().
The "Value" argument is interpreted as an integer, but can also be specified as a character string.
If "Value" is not an integer, 0 is returned.
If "to Base" is a negative number, "Value" is considered a signed number, otherwise unsigned.
CONV() works with 64-bit precision.
Specifying hexadecimal and binary literal values in argument only works to a limited extent in MariaDB:
0xffff doesn't work, but 0xffff + 0 does. See also examples. | Examples running on MariaDB 10.5 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
|
select conv(10, 10, 16);
select conv(10, 10, 8);
select conv(10, 10, 4);
select conv(10, 10, 2);
select conv('10', 10, 2);
select conv(-10, 10, 8);
select conv(-10, 10, -8);
select conv('ff', 16, 10);
select conv('ff00', 16, 10);
select conv('1111', 2, 10);
select conv('1111', 2, 16);
select conv(0xff00, 10, 10); /* works not in MariaDB*/
select conv(0xff00 + 0, 10, 10); /* works */
select conv(0b1111, 10, 10); /* works not in MariaDB*/
select conv(0b1111 + 0, 10, 10); /* works */
select conv('test', 10, 2);
select conv(null, 10, 2);
select conv(10, 10, 1);
|
|
conv(10, 10, 16) |
varchar(64) BINARY |
A |
|
|
conv(10, 10, 8) |
varchar(64) BINARY |
12 |
|
|
conv(10, 10, 4) |
varchar(64) BINARY |
22 |
|
|
conv(10, 10, 2) |
varchar(64) BINARY |
1010 |
|
|
conv('10', 10, 2) |
varchar(64) BINARY |
1010 |
|
|
conv(-10, 10, 8) |
varchar(64) BINARY |
1777777777777777777766 |
|
|
conv(-10, 10, -8) |
varchar(64) BINARY |
-12 |
|
|
conv('ff', 16, 10) |
varchar(64) BINARY |
255 |
|
|
conv('ff00', 16, 10) |
varchar(64) BINARY |
65280 |
|
|
conv('1111', 2, 10) |
varchar(64) BINARY |
15 |
|
|
conv('1111', 2, 16) |
varchar(64) BINARY |
F |
|
|
conv(0xff00, 10, 10) |
varchar(64) BINARY |
0 |
|
|
conv(0xff00 + 0, 10, 10) |
varchar(64) BINARY |
65280 |
|
|
conv(0b1111, 10, 10) |
varchar(64) BINARY |
0 |
|
|
conv(0b1111 + 0, 10, 10) |
varchar(64) BINARY |
15 |
|
|
conv('test', 10, 2) |
varchar(64) BINARY |
0 |
|
|
conv(null, 10, 2) |
varchar(64) BINARY |
NULL |
|
|
conv(10, 10, 1) |
varchar(64) BINARY |
NULL |
|
|
| Examples running on MySQL 8.0.23 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
|
select conv(10, 10, 16);
select conv(10, 10, 8);
select conv(10, 10, 4);
select conv(10, 10, 2);
select conv('10', 10, 2);
select conv(-10, 10, 8);
select conv(-10, 10, -8);
select conv('ff', 16, 10);
select conv('ff00', 16, 10);
select conv('1111', 2, 10);
select conv('1111', 2, 16);
select conv(0xff00, 10, 10); /* works on MySQL 8.0 */
select conv(0xff00 + 0, 10, 10); /* works */
select conv(0b1111, 10, 10); /* works on MySQL 8.0 */
select conv(0b1111 + 0, 10, 10); /* works */
select conv('test', 10, 2);
select conv(null, 10, 2);
select conv(10, 10, 1);
|
|
conv(10, 10, 16) |
varchar(65) BINARY |
A |
|
|
conv(10, 10, 8) |
varchar(65) BINARY |
12 |
|
|
conv(10, 10, 4) |
varchar(65) BINARY |
22 |
|
|
conv(10, 10, 2) |
varchar(65) BINARY |
1010 |
|
|
conv('10', 10, 2) |
varchar(65) BINARY |
1010 |
|
|
conv(-10, 10, 8) |
varchar(65) BINARY |
1777777777777777777766 |
|
|
conv(-10, 10, -8) |
varchar(65) BINARY |
-12 |
|
|
conv('ff', 16, 10) |
varchar(65) BINARY |
255 |
|
|
conv('ff00', 16, 10) |
varchar(65) BINARY |
65280 |
|
|
conv('1111', 2, 10) |
varchar(65) BINARY |
15 |
|
|
conv('1111', 2, 16) |
varchar(65) BINARY |
F |
|
|
conv(0xff00, 10, 10) |
varchar(65) BINARY |
65280 |
|
|
conv(0xff00 + 0, 10, 10) |
varchar(65) BINARY |
65280 |
|
|
conv(0b1111, 10, 10) |
varchar(65) BINARY |
15 |
|
|
conv(0b1111 + 0, 10, 10) |
varchar(65) BINARY |
15 |
|
|
conv('test', 10, 2) |
varchar(65) BINARY |
0 |
|
|
conv(null, 10, 2) |
varchar(65) BINARY |
NULL |
|
|
conv(10, 10, 1) |
varchar(65) BINARY |
NULL |
|
|
| The examples were created with the MyWAY SQL manager: | How to use the CONV() function in MySQL and MariaDB databases | The CONV() function in MySQL and MariaDB is used to convert a number from a numeric base to another by specifying the input value, the base of the input value, and the base to convert the value to. The result is returned as a string. The function supports various numeric systems, including binary (base 2), octal (base 8), decimal (base 10) and hexadecimal (base 16). The function supports conversions between bases 2 to 36 and can, for example, work with binary or hexadecimal representations of data. Various applications are enabled, such as basic conversion, binary data manipulation, encoding and decoding and display formatting. | | Further MySQL und MariaDB SQL Conversion functions | |
| | More information about the CONV SQL function: and and |
|
|
|
|