Tuesday, December 24, 2024

MySQL CONV() Function

 The CONV() function in MySQL is used to convert a number from one base to another. It can be particularly useful when dealing with different numeral systems such as binary, octal, decimal, or hexadecimal.

Syntax:

CONV(N, from_base, to_base)
  • N: The number to be converted. It must be a string or a number.
  • from_base: The base of the input number. It must be an integer between 2 and 36.
  • to_base: The base to which the number should be converted. It must also be an integer between 2 and 36.

Returns:

  • A string representing the number in the target base.

Example:

  1. Convert a hexadecimal number (1F) to a decimal:

    SELECT CONV('1F', 16, 10);
    

    Result: 31 (since 1F in hexadecimal equals 31 in decimal).

  2. Convert a decimal number (31) to binary:

    SELECT CONV('31', 10, 2);
    

    Result: 11111 (31 in decimal is 11111 in binary).

  3. Convert a decimal number (31) to hexadecimal:

    SELECT CONV('31', 10, 16);
    

    Result: 1F (31 in decimal is 1F in hexadecimal).

Notes:

  • The CONV() function works with bases from 2 to 36.
  • The output is always returned as a string.

No comments:

Post a Comment