Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert an IP address into an integer with Lua?

In Lua how would I go about converting a string containing an IP address into an integer?

like image 568
Marco Avatar asked Nov 20 '11 08:11

Marco


People also ask

How do you change a string to an int in Lua?

Conversion. You can convert strings to numbers using the function tonumber() . This takes a string argument and returns a number.

Is IP address integer or string?

The following constitutes a valid IPv4 address: A string in decimal-dot notation, consisting of four decimal integers in the inclusive range 0–255, separated by dots (e.g. 192.168. 0.1 ). Each integer represents an octet (byte) in the address.


1 Answers

IPv4 I assume? and how do you want it as an integer? maybe:

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 num = 2^24*o1 + 2^16*o2 + 2^8*o3 + o4
like image 88
daurnimator Avatar answered Oct 04 '22 19:10

daurnimator