Hexadecimal literal values are written using
X'
or
val
'0x
notation,
where val
val
contains hexadecimal digits
(0..9
, A..F
). Lettercase
of the digits and of any leading X
does not
matter. A leading 0x
is case-sensitive and
cannot be written as 0X
.
Legal hexadecimal literals:
X'01AF'
X'01af'
x'01AF'
x'01af'
0x01AF
0x01af
Illegal hexadecimal literals:
X'0G' (G is not a hexadecimal digit)
0X01AF (0X must be written as 0x)
Values written using
X'
notation
must contain an even number of digits or a syntax error occurs.
To correct the problem, pad the value with a leading zero:
val
'
mysql> SET @s = X'FFF';
ERROR 1064 (42000): You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server
version for the right syntax to use near 'X'FFF''
mysql> SET @s = X'0FFF';
Query OK, 0 rows affected (0.00 sec)
Values written using
0x
notation
that contain an odd number of digits are treated as having an
extra leading val
0
. For example,
0xaaa
is interpreted as
0x0aaa
.
By default, a hexadecimal literal is a binary string, where each pair of hexadecimal digits represents a character:
mysql> SELECT X'4D7953514C', CHARSET(X'4D7953514C');
+---------------+------------------------+
| X'4D7953514C' | CHARSET(X'4D7953514C') |
+---------------+------------------------+
| MySQL | binary |
+---------------+------------------------+
mysql> SELECT 0x5461626c65, CHARSET(0x5461626c65);
+--------------+-----------------------+
| 0x5461626c65 | CHARSET(0x5461626c65) |
+--------------+-----------------------+
| Table | binary |
+--------------+-----------------------+
A hexadecimal literal may have an optional character set
introducer and COLLATE
clause, to designate
it as a string that uses a particular character set and
collation:
[_charset_name] X'val' [COLLATE collation_name]
Examples:
SELECT _latin1 X'4D7953514C';
SELECT _utf8mb4 0x4D7953514C COLLATE utf8mb4_danish_ci;
The examples use
X'
notation,
but val
'0x
notation
permits introducers as well. For information about introducers,
see Section 12.3.8, “Character Set Introducers”.
val
In numeric contexts, MySQL treats a hexadecimal literal like a
BIGINT UNSIGNED
(64-bit unsigned integer). To
ensure numeric treatment of a hexadecimal literal, use it in
numeric context. Ways to do this include adding 0 or using
CAST(... AS UNSIGNED)
. For
example, a hexadecimal literal assigned to a user-defined
variable is a binary string by default. To assign the value as a
number, use it in numeric context:
mysql> SET @v1 = X'41';
mysql> SET @v2 = X'41'+0;
mysql> SET @v3 = CAST(X'41' AS UNSIGNED);
mysql> SELECT @v1, @v2, @v3;
+------+------+------+
| @v1 | @v2 | @v3 |
+------+------+------+
| A | 65 | 65 |
+------+------+------+
An empty hexadecimal value (X''
) evaluates to
a zero-length binary string. Converted to a number, it produces
0:
mysql> SELECT CHARSET(X''), LENGTH(X'');
+--------------+-------------+
| CHARSET(X'') | LENGTH(X'') |
+--------------+-------------+
| binary | 0 |
+--------------+-------------+
mysql> SELECT X''+0;
+-------+
| X''+0 |
+-------+
| 0 |
+-------+
The X'
notation is based on standard SQL. The val
'0x
notation is based on ODBC, for which hexadecimal strings are
often used to supply values for
BLOB
columns.
To convert a string or a number to a string in hexadecimal
format, use the HEX()
function:
mysql> SELECT HEX('cat');
+------------+
| HEX('cat') |
+------------+
| 636174 |
+------------+
mysql> SELECT X'636174';
+-----------+
| X'636174' |
+-----------+
| cat |
+-----------+
For hexadecimal literals, bit operations are considered numeric
context, but bit operations permit numeric or binary string
arguments in MySQL 9.1 and higher. To explicitly
specify binary string context for hexadecimal literals, use a
_binary
introducer for at least one of the
arguments:
mysql> SET @v1 = X'000D' | X'0BC0';
mysql> SET @v2 = _binary X'000D' | X'0BC0';
mysql> SELECT HEX(@v1), HEX(@v2);
+----------+----------+
| HEX(@v1) | HEX(@v2) |
+----------+----------+
| BCD | 0BCD |
+----------+----------+
The displayed result appears similar for both bit operations,
but the result without _binary
is a
BIGINT
value, whereas the result with
_binary
is a binary string. Due to the
difference in result types, the displayed values differ:
High-order 0 digits are not displayed for the numeric result.