Lua ip转整数

 

function _M.ipToInt( str )
    local num = 0
    if str and type(str)=="string" then
        local o1,o2,o3,o4 = str:match("(%d+)%.(%d+)%.(%d+)%.(%d+)" )
        num = 2^24*o1 + 2^16*o2 + 2^8*o3 + o4
    end
    return num
end
-- inter to ip
function _M.intToIp( n )
    if n then
        n = tonumber(n)
        local n1 = math.floor(n / (2^24)) 
        local n2 = math.floor((n - n1*(2^24)) / (2^16))
        local n3 = math.floor((n - n1*(2^24) - n2*(2^16)) / (2^8))
        local n4 = math.floor((n - n1*(2^24) - n2*(2^16) - n3*(2^8)))
        return n1.."."..n2.."."..n3.."."..n4 
    end
    return "0.0.0.0"
end

转载于:https://my.oschina.net/ahuaahua/blog/1596110

posted @ 2020-08-20 15:42  笠航  阅读(807)  评论(0编辑  收藏  举报