数据库
首页 > 数据库> > mysql HEX将字符串或数字转化为16进制字符串、UNHEX将16字符串转化成二进制

mysql HEX将字符串或数字转化为16进制字符串、UNHEX将16字符串转化成二进制

作者:互联网

目录

前言

HEX

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

HEX 函数的参数为数字时

函数作用:数字参数N(十进制) -> 十六进制的数值=CONV(N,10,16) -> 将十六进制的数值转化为字符串

mysql> select HEX(48);
+---------+
| HEX(48) |
+---------+
| 30      |
+---------+
1 row in set (0.01 sec)
mysql> select HEX(0x30);
+-----------+
| HEX(0x30) |
+-----------+
| 30        |
+-----------+
1 row in set (0.01 sec)

HEX 函数的参数为字符串时

函数作用:字符串参数S -> 字符串中的每个字符(按照ASCII码表)转化成ASCII码(十六进制数值) -> 将十六进制的数值转化为字符串

mysql> select HEX('0');
+----------+
| HEX('0') |
+----------+
| 30       |
+----------+
1 row in set (0.01 sec)

UNHEX

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)

HEX 转化 binary 类型

在这里插入图片描述

标签:UNHEX,十六进制,16,HEX,0x30,mysql,字符串
来源: https://blog.csdn.net/sayyy/article/details/121652620