In Lua how would I go about converting a string containing an IP address into an integer?
Conversion. You can convert strings to numbers using the function tonumber() . This takes a string argument and returns a number.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With