w3resource
MYSQL Tutorial

MySQL HEX() function

HEX() function

MySQL HEX() returns a string representation of a hexadecimal value of a decimal or string value specified as an argument.

If the argument is a string, each character in the argument is converted to two hexadecimal digits.

If the argument is decimal, the function returns a hexadecimal string representation of the argument and treated as a longlong(BIGINT) number.

This function is equivalent to CONV(N,10,16).

Syntax:

HEX (N or S)

Arguments

Name Description
N A number which is to be converted to hexadecimal.
S A string whose each character is to be converted to two hexadecimal digits.

MySQL Version: 5.6

Video Presentation

Example : MySQL HEX() function

In the following MySQL statement, the argument 157 is a number, which is converted to a hexadecimal number. The output is 9D.

Code

SELECT HEX(157); 

Sample Output:

mysql> SELECT HEX(157);
+----------+
| HEX(157) |
+----------+
| 9D       | 
+----------+
1 row in set (0.00 sec)

Example of MySQL HEX() function on character value

In the above MySQL statement, the argument 'Q' is a string. The character of the string is converted to two hexadecimal digits. The output is 51.

Code

SELECT HEX(‘Q’); 

Sample Output:

mysql> SELECT HEX('Q');
+----------+
| HEX('Q') |
+----------+
| 51       | 
+----------+
1 row in set (0.00 sec)

All String Functions

MySQL String Functions, slide presentation

Many of our valued users post comments along with piece of code. Disqus may, false positively, consider those piece of code as Bad or Starnge syntax and send those comments to spam automatically. Even if that happens, we will make sure those useful comments are taken out of spam and approved. It may take a couple of days for that though, but any useful comment will be brought to public view for sure. We regret if that happend to any user. You may write us directly regarding this to - w3resource[at]yahoo[dot]com.