MySQL supports hexadecimal values, written using
X',
val'x', or
val'0x format,
where valval contains hexadecimal digits
(0..9, A..F). Lettercase
of the digits does not matter. For values written using
X' or
val'x' format,
val'val must contain an even number of
digits. For values written using
0x,
values that contain an odd number of digits are treated as
having an extra leading val syntax0. For example,
0x0a and 0xaaa are
interpreted as 0x0a and
0x0aaa.
In numeric contexts, hexadecimal values act like integers (64-bit precision). In string contexts, they act like binary strings, where each pair of hex digits is converted to a character:
mysql>SELECT X'4D7953514C';-> 'MySQL' mysql>SELECT 0x0a+0;-> 10 mysql>SELECT 0x5061756c;-> 'Paul'
In MySQL 4.1 (and in MySQL 4.0 when using the
--new option), the default type of a
hexadecimal value is a string. If you want to ensure that the
value is treated as a number, you can use
CAST(... AS UNSIGNED):
mysql> SELECT 0x41, CAST(0x41 AS UNSIGNED);
-> 'A', 65
The X'
syntax is new in 4.0 and is based on standard SQL. The
hexstring'0x syntax is based on ODBC. Hexadecimal
strings are often used by ODBC to supply values for
BLOB columns.
Beginning with MySQL 4.0.1, you can convert a string or a number
to a string in hexadecimal format with the
HEX() function:
mysql>SELECT HEX('cat');-> '636174' mysql>SELECT 0x636174;-> 'cat'

User Comments
Add your own comment.