HEX 函数的官方说明如下:
HEX(str), HEX(N)
For a string argument str, HEX() returns a hexadecimal string representation of str where each byte of each character in str is converted to two hexadecimal digits. (Multibyte characters therefore become more than two digits.) The inverse of this operation is performed by the UNHEX() function.
For a numeric argument N, HEX() returns a hexadecimal string representation of the value of N treated as a longlong (BIGINT) number. This is equivalent to CONV(N,10,16). The inverse of this operation is performed by CONV(HEX(N),16,10).
mysql> SELECT X'616263', HEX('abc'), UNHEX(HEX('abc')); -> 'abc', 616263, 'abc' mysql> SELECT HEX(255), CONV(HEX(255),16,10); -> 'FF', 255
函数作用:数字参数N(十进制) -> 十六进制的数值=CONV(N,10,16) -> 将十六进制的数值转化为字符串
mysql> select HEX(48); +---------+ | HEX(48) | +---------+ | 30 | +---------+ 1 row in set (0.01 sec)
'0'
对应的十六进制数字为0x30
。十六进制数字0x30
对应的十进制数字为48。mysql> select HEX(0x30); +-----------+ | HEX(0x30) | +-----------+ | 30 | +-----------+ 1 row in set (0.01 sec)
'0'
对应的十六进制数字为0x30
。十六进制数字0x30
对应的十进制数字为48。函数作用:字符串参数S -> 字符串中的每个字符(按照ASCII码表)转化成ASCII码(十六进制数值) -> 将十六进制的数值转化为字符串
mysql> select HEX('0'); +----------+ | HEX('0') | +----------+ | 30 | +----------+ 1 row in set (0.01 sec)
'0'
对应的十六进制数字为0x30
。十六进制数字0x30
对应的十进制数字为48。UNHEX函数的官方说明如下:
UNHEX(str)
For a string argument str, UNHEX(str) interprets each pair of characters in the argument as a hexadecimal number and converts it to the byte represented by the number. The return value is a binary string.
mysql> SELECT UNHEX('4D7953514C'); -> 'MySQL' mysql> SELECT X'4D7953514C'; -> 'MySQL' mysql> SELECT UNHEX(HEX('string')); -> 'string' mysql> SELECT HEX(UNHEX('1267')); -> '1267'
mysql> select UNHEX('30'); +-------------+ | UNHEX('30') | +-------------+ | 0 | +-------------+ 1 row in set (0.02 sec)
'0'
对应的十六进制数字为0x30
。十六进制数字0x30
对应的十进制数字为48。