aserto wrote:Doesn't work.... :(
Maybe you can write a bit more about what doesn't work. Lua Player 0.20 works? Then this works, too:
Code: Select all
function ip2string(ip)
local string = nil
for i = 1, 4 do
if string then string = string .. "." else string = "" end
string = string .. ip[i]
end
return string
end
function ip2number(ip)
local number = 0
local m = 1
for i = 1, 4 do
number = number + ip[i] * m
m = m * 256
end
return number
end
function number2ip(number)
local ip = {}
local m = 256
for i = 1, 4 do
ip[i] = number % m
number = math.floor(number / 256)
end
setmetatable(ip, { __tostring = ip2string })
return ip
end
tests = {
{ { 0, 0, 0, 0 }, 0 },
{ { 0, 0, 0, 1 }, 16777216 },
{ { 1, 0, 0, 0 }, 1 },
{ { 1, 2, 3, 4 }, 67305985 },
{ { 4, 3, 2, 1 }, 16909060 },
{ { 212, 227, 39, 202 }, 3391611860 },
{ { 255, 255, 255, 255 }, 4294967295 } }
y = 0
for _, test in pairs(tests) do
ip, expected = test[1], test[2]
number = ip2number(ip)
assert(number == expected)
ip2 = number2ip(number)
assert(ip[1] == ip2[1] and ip[2] == ip2[2] and ip[3] == ip2[3] and ip[4] == ip2[4])
screen:print(0, y, tostring(ip2), Color.new(255, 255, 255))
y = y + 10
end
screen.waitVblankStart()
screen.flip()
while true do
screen.waitVblankStart()
if Controls.read():start() then break end
end
On my PSP it prints this:
0.0.0.0
0.0.0.1
1.0.0.0
1.2.3.4
4.3.2.1
212.227.39.202
255.255.255.255