十六进制字面量值可以使用 X' 或 val'0x 表示,其中 valval 包含十六进制数字(0..9, A..F)。数字的字母大小写以及前导 X 的大小写无关紧要。前导 0x 是区分大小写的,不能写成 0X。Hexadecimal literal values are written using
X' or
val'0x notation,
where valval 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)
使用 X' 表示法书写的值必须包含偶数位数字,否则会发生语法错误。要解决此问题,请在值前加一个前导零:Values written using
val'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)
使用 0x 表示法书写的值如果包含奇数位数字,则会被视为具有额外的前导 val0。例如,0xaaa 被解释为 0x0aaa。Values written using
0x notation
that contain an odd number of digits are treated as having an
extra leading val0. 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 | +--------------+-----------------------+
十六进制字面量可以有可选的字符集引导符和 COLLATE 子句,以将其指定为使用特定字符集和排序规则的字符串: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' [COLLATEcollation_name]
示例:Examples:
SELECT _latin1 X'4D7953514C'; SELECT _utf8mb4 0x4D7953514C COLLATE utf8mb4_danish_ci;
示例使用 X' 表示法,但 val'0x 表示法也允许使用引导符。有关引导符的信息,请参阅 12.3.8 节,“字符集引导符”。The examples use
valX' notation,
but val'0x notation
permits introducers as well. For information about introducers,
see Section 12.3.8, “Character Set Introducers”.val
在数值上下文中,MySQL 将十六进制字面量视为 BIGINT UNSIGNED(64 位无符号整数)。为了确保十六进制字面量以数值方式处理,请在数值上下文中使用它。实现的方法包括加 0 或使用 CAST(... AS UNSIGNED)。例如,赋值给用户自定义变量的十六进制字面量默认是二进制字符串。要将值作为数字赋值,请在数值上下文中使用它: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 | +------+------+------+
空的十六进制值(X'')会被评估为零长度二进制字符串。转换为数字时,会得到 0: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 | +-------+
X' 表示法是基于标准 SQL 的。val'0x 表示法是基于 ODBC 的,其中十六进制字符串通常用于为 BLOB 列提供值。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.
要将字符串或数字转换为十六进制格式的字符串,请使用 HEX() 函数: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 | +-----------+
对于十六进制字面值,位运算被视为数字上下文,但在 MySQL 8.4 及更高版本中,位运算允许数字或二进制字符串参数。要为十六进制字面值显式指定二进制字符串上下文,请至少为其中一个参数使用 _binary 引导符:For hexadecimal literals, bit operations are considered numeric
context, but bit operations permit numeric or binary string
arguments in MySQL 8.4 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 | +----------+----------+
显示的结果对于这两种位操作看起来相似,但没有 _binary 的结果是 BIGINT 值,而使用 _binary 的结果是二进制字符串。由于结果类型的不同,显示的值也不同:数字结果不会显示高位的 0。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.