0

Any easy way to detect if an IP is a valid IPv6 and also if it is valid, how to parse it to get the byte array?

Currently using ZeroBrane Studio IDE and the integrated Lua version is 5.1

Vlam
  • 1,622
  • 1
  • 8
  • 17
  • Possible duplicate of [Lua function check if ipv4 or ipv6 or string](http://stackoverflow.com/questions/10975935/lua-function-check-if-ipv4-or-ipv6-or-string) – hjpotter92 Sep 06 '16 at 16:32

1 Answers1

1

As part of the answer on how to check if the IPv6 address is valid, the string is parsed into a table, which you can use for your purposes:

-- assuming you already check that the string has IPv6 format
local ip = "1050:0000:0000:0000:0005:0600:300c:326b"
local chunks = {ip:match(("([a-fA-F0-9]*):"):rep(8):gsub(":$","$"))}
print(table.concat(chunks, ":"))
Community
  • 1
  • 1
Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56