MySQL中实现IP地址转换为整数、整数转换为IP地址、十六进制转为十进制、十进制转为十六进制的函数整理如下:
IP地址转换为整数
mysql> SELECT INET_ATON('172.21.10.31');
+---------------------------+
| INET_ATON('172.21.10.31') |
+---------------------------+
| 2887059999 |
+---------------------------+
1 row in set (0.08 sec)
整数转换为IP地址
mysql> SELECT INET_NTOA(2887059999);
+-----------------------+
| INET_NTOA(2887059999) |
+-----------------------+
| 172.21.10.31 |
+-----------------------+
1 row in set (0.00 sec)
十六进制转为十进制
mysql> select CONV('A43BB57',16,10);
+-----------------------+
| CONV('A43BB57',16,10) |
+-----------------------+
| 172211031 |
+-----------------------+
1 row in set (0.06 sec)
十进制转为十六进制
mysql> select CONV('172211031',10,16);
+-------------------------+
| CONV('172211031',10,16) |
+-------------------------+
| A43BB57 |
+-------------------------+
1 row in set (0.00 sec)
参考原文:mysql中ip和整数的转换