2 -- Copyright (c) 2020 Intel Corporation
4 -- Licensed under the Apache License, Version 2.0 (the "License");
5 -- you may not use this file except in compliance with the License.
6 -- You may obtain a copy of the License at
8 -- http://www.apache.org/licenses/LICENSE-2.0
10 -- Unless required by applicable law or agreed to in writing, software
11 -- distributed under the License is distributed on an "AS IS" BASIS,
12 -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 -- See the License for the specific language governing permissions and
14 -- limitations under the License.
17 function convertIPToHex(ip)
18 local address_chunks = {}
19 if type(ip) ~= "string" then
20 print ("IP ADDRESS ERROR: ", ip)
21 return "IP ADDRESS ERROR"
24 local chunks = {ip:match("^(%d+)%.(%d+)%.(%d+)%.(%d+)$")}
26 for i,v in ipairs(chunks) do
27 if tonumber(v) > 255 then
28 print ("IPV4 ADDRESS ERROR: ", ip)
29 return "IPV4 ADDRESS ERROR"
31 address_chunks[#address_chunks + 1] = string.format ("%02x", v)
33 result = table.concat(address_chunks, " ")
34 print ("Hex IPV4: ", result)
39 for match in (ip..delimiter):gmatch("(.-)"..delimiter) do
41 number = tonumber(match, 16)
42 if number <= 65535 then
43 table.insert(address_chunks, string.format("%02x %02x",number/256,number % 256))
46 table.insert(address_chunks, "")
49 for i, chunk in ipairs(address_chunks) do
51 table.remove(address_chunks, i)
52 for j = 1,(8-#address_chunks) do
53 table.insert(address_chunks, i, "00 00")
58 result = table.concat(address_chunks, " ")
59 print ("Hex IPV6: ", result)