openresty-IP字符串转数字,openresty-ip字符串


1. IP字符串转换成数字

local str = "127.0.0.1"
local o1,o2,o3,o4 = str:match("(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)" )
-- local o1,o2,o3,o4 = str:match("(%d+)%.(%d+)%.(%d+)%.(%d+)")

local num = 2^24*o1 + 2^16*o2 + 2^8*o3 + o4

2. 数字转IP字符串

todo

3. 用SQL更方便

mysql> SELECT INET_ATON('209.207.224.40');
+-----------------------------+
| INET_ATON('209.207.224.40') |
+-----------------------------+
|                  3520061480 |
+-----------------------------+
1 row in set (0.00 sec)

mysql> SELECT INET_NTOA(3520061480);
+-----------------------+
| INET_NTOA(3520061480) |
+-----------------------+
| 209.207.224.40        |
+-----------------------+
1 row in set (0.00 sec)

mysql> 

ref

http://stackoverflow.com/questions/8200228/how-can-i-convert-an-ip-address-into-an-integer-with-lua

相关内容

    暂无相关文章