如何使用Lua将整数转换为IP地址?

时间:2018-09-09 18:42:10

标签: lua ip

我有这个2709862549,并想将其转换为字符串IP,例如149.56.133.161 在LUA中有没有为此功能的功能,我无法搜索该信息。

1 个答案:

答案 0 :(得分:0)

已经找到

function intToIp(n)
    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 n4.."."..n3.."."..n2.."."..n1
end
相关问题