Tuesday 30 July 2019

How to “Unhex” a Number in MySQL

In MySQL, you can unhex a string using the UNHEX() function. But you can’t unhex a number with that function.
To unhex a number in MySQL, use the CONV() function instead.
The CONV() function allows you to convert numeric values between different numbering systems. For example, you can convert between say decimal and binary, octal to decimal, or, more relevant to this article, between hexadecimal and decimal.

Syntax

The syntax goes like this:
CONV(N,from_base,to_base)
Where N is the number you want to convert, from_base is the base that the number is in, and to_base is the base you want to convert it to.

Example

Here’s an example to demonstrate:
SELECT CONV('F',16,10);
Result:
+-----------------+
| CONV('F',16,10) |
+-----------------+
| 15              |
+-----------------+
In this case, we convert the number F from base 16 (hexadecimal) to base 10 (decimal). So we can see that F in hexadecimal equals 15 in decimal.
Here’s another example using a larger number:
SELECT CONV('FCA3B',16,10);
Result:
+---------------------+
| CONV('FCA3B',16,10) |
+---------------------+
| 1034811             |
+---------------------+

0 comments:

Post a Comment