9.1.4 Hexadecimal Literals十六进制文字

Hexadecimal literal values are written using X'val' or 0xval notation, where val contains hexadecimal digits (0..9, A..F). 十六进制文字值使用X'val'0xval表示法编写,其中val包含十六进制数字(0..9A..F)。Lettercase of the digits and of any leading X does not matter. 数字和任何前导X的大小写都无关紧要。A leading 0x is case-sensitive and cannot be written as 0X.前导0x区分大小写,不能写成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'val' notation must contain an even number of digits or a syntax error occurs. To correct the problem, pad the value with a leading zero:使用X'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 0xval notation that contain an odd number of digits are treated as having an extra leading 0. For example, 0xaaa is interpreted as 0x0aaa.使用0xval表示法写入的包含奇数位数的值将被视为具有额外的前导0。例如,0xaaa被解释为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:十六进制文字可能有一个可选的字符集导入器和COLLATE子句,以将其指定为使用特定字符集和排序规则的字符串:

[_charset_name] X'val' [COLLATE collation_name]

Examples:示例:

SELECT _latin1 X'4D7953514C';
SELECT _utf8 0x4D7953514C COLLATE utf8_danish_ci;

The examples use X'val' notation, but 0xval notation permits introducers as well. 示例使用了X'val'表示法,但0xval表示法也允许介绍人使用。For information about introducers, see Section 10.3.8, “Character Set Introducers”.有关介绍人的信息,请参阅第10.3.8节,“字符集引入器”

In numeric contexts, MySQL treats a hexadecimal literal like a BIGINT UNSIGNED (64-bit unsigned integer). 在数字上下文中,MySQL将十六进制文字视为BIGINT UNSIGNED(64位无符号整数)。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). 为了确保十六进制文字的数字处理,请在数字上下文中使用它。这样做的方法包括添加0或使用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:空的十六进制值(X'')计算结果为零长度的二进制字符串。转换为数字后,它将产生0:

mysql> SELECT CHARSET(X''), LENGTH(X'');
+--------------+-------------+
| CHARSET(X'') | LENGTH(X'') |
+--------------+-------------+
| binary       |           0 |
+--------------+-------------+
mysql> SELECT X''+0;
+-------+
| X''+0 |
+-------+
|     0 |
+-------+

The X'val' notation is based on standard SQL. X'val'表示法基于标准SQL。The 0x notation is based on ODBC, for which hexadecimal strings are often used to supply values for BLOB columns.0x表示法基于ODBC,通常使用十六进制字符串为BLOB列提供值。

To convert a string or a number to a string in hexadecimal format, use the HEX() function:要将字符串或数字转换为十六进制格式的字符串,请使用HEX()函数:

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 8.0 and higher. 对于十六进制文字,位操作被视为数字上下文,但在MySQL 8.0及更高版本中,位操作允许数字或二进制字符串参数。To explicitly specify binary string context for hexadecimal literals, use a _binary introducer for at least one of the arguments:要显式指定十六进制文字的二进制字符串上下文,请对至少一个参数使用_binary引入器:

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. 对于两个位运算,显示的结果看起来相似,但不带_binary的结果是BIGINT值,而带_binaary的结果是二进制字符串。Due to the difference in result types, the displayed values differ: High-order 0 digits are not displayed for the numeric result.由于结果类型的不同,显示的值也不同:数值结果不显示高位0位数字。