Databases 7 min read

Understanding MySQL HEX/UNHEX Discrepancies and Disabling Binary‑as‑Hex Output

The article explains why MySQL's HEX and UNHEX functions sometimes return hexadecimal representations instead of plain strings, demonstrates the issue with example queries, clarifies the meaning of related functions, and shows how to disable the client’s binary‑as‑hex option to get correct output.

Aikesheng Open Source Community
Aikesheng Open Source Community
Aikesheng Open Source Community
Understanding MySQL HEX/UNHEX Discrepancies and Disabling Binary‑as‑Hex Output

A netizen reported that the results of MySQL's HEX and UNHEX functions differed from the official manual examples.

The manual shows:

mysql> SELECT X'616263', HEX('abc'), UNHEX(HEX('abc'));
--> 'abc', 616263, 'abc'
mysql> SELECT HEX(255), CONV(HEX(255),16,10);
--> 'FF', 255

But the user observed:

mysql>SELECT X'616263', HEX('abc'), UNHEX(HEX('abc'));
+----------------------+------------+--------------------------------------+
| X'616263'            | HEX('abc') | UNHEX(HEX('abc'))                    |
+----------------------+------------+--------------------------------------+
| 0x616263             | 616263     | 0x616263                             |
+----------------------+------------+--------------------------------------+
1 row in set (0.00 sec)

mysql>SELECT HEX(255), CONV(HEX(255),16,10);
+----------+----------------------+ 
| HEX(255) | CONV(HEX(255),16,10) |
+----------+----------------------+ 
| FF       | 255                  |
+----------+----------------------+ 
1 row in set (0.00 sec)

The first query’s X'616263' and UNHEX(HEX('abc')) results differ from the manual, while the second query matches.

Key function meanings:

HEX converts a string or number to its hexadecimal representation.

UNHEX performs the reverse conversion.

CONV converts numbers between different bases.

The X'…' literal (or 0x… ) denotes a hexadecimal string, case‑insensitive.

Running the same queries on the author's VM produced the same unexpected hexadecimal output:

mysql:ytt:8.0.28>select X'616263',UNHEX(HEX('abc'));
+----------------------+--------------------------------------+
| X'616263'            | UNHEX(HEX('abc'))                    |
+----------------------+--------------------------------------+
| 0x616263             | 0x616263                             |
+----------------------+--------------------------------------+
1 row in set (0.00 sec)

The MySQL client status shows Binary data as: Hexadecimal , meaning binary data is printed as hex by default.

Both values have the binary charset, so converting them to UTF‑8 yields the correct string:

mysql:(none):8.0.28>select convert(X'616263' using utf8mb4),convert(UNHEX(HEX('abc')) using utf8mb4);
+----------------------------------+------------------------------------------+
| convert(X'616263' using utf8mb4) | convert(UNHEX(HEX('abc')) using utf8mb4) |
+----------------------------------+------------------------------------------+
| abc                              | abc                                      |
+----------------------------------+------------------------------------------+
1 row in set (0.00 sec)

The underlying cause is the MySQL client option --binary-as-hex , which is enabled for interactive terminals and prints binary data as hex.

Disabling the option either in the my.cnf under the [mysql] section or via the command line fixes the output:

root@ytt-ubuntu:~# mysql --binary-as-hex=false
...
mysql:(none):8.0.28>select X'616263',UNHEX(HEX('abc'));
+-----------+-------------------+
| X'616263' | UNHEX(HEX('abc')) |
+-----------+-------------------+
| abc       | abc               |
+-----------+-------------------+
1 row in set (0.00 sec)

The equivalent shortcut --skip-binary-as-hex produces the same result.

root@ytt-ubuntu:~# mysql --skip-binary-as-hex
...
mysql:(none):8.0.28>select X'616263',UNHEX(HEX('abc'));
+-----------+-------------------+
| X'616263' | UNHEX(HEX('abc')) |
+-----------+-------------------+
| abc       | abc               |
+-----------+-------------------+
1 row in set (0.00 sec)

Note that this option also affects the display of BLOB and BINARY column data.

SQLDatabaseMySQLHEXbinary-as-hexUNHEX
Aikesheng Open Source Community
Written by

Aikesheng Open Source Community

The Aikesheng Open Source Community provides stable, enterprise‑grade MySQL open‑source tools and services, releases a premium open‑source component each year (1024), and continuously operates and maintains them.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.