Hexadecimal literal values are written using 十六进制文字值使用X'
or val
'0x
notation, where val
val
contains hexadecimal digits (0..9
, A..F
). X'val'
或0xval
表示法编写,其中val
包含十六进制数字(0..9
、A..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'
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
'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 使用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
.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
' [COLLATEcollation_name
]
Examples:示例:
SELECT _latin1 X'4D7953514C'; SELECT _utf8 0x4D7953514C COLLATE utf8_danish_ci;
The examples use 示例使用了X'
notation, but val
'0x
notation permits introducers as well. val
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 在数字上下文中,MySQL将十六进制文字视为BIGINT UNSIGNED
(64-bit unsigned integer). 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 为了确保十六进制文字的数字处理,请在数字上下文中使用它。这样做的方法包括添加0或使用CAST(... AS UNSIGNED)
. 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'
notation is based on standard SQL. val
'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位数字。