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+)(\/%d+)$")}
26 for i,v in ipairs(chunks) do
28 if tonumber(v) > 255 then
29 print ("IPV4 ADDRESS ERROR: ", ip)
30 return "IPV4 ADDRESS ERROR"
32 address_chunks[#address_chunks + 1] = string.format ("%02x", v)
35 result = table.concat(address_chunks, " ")
36 print ("Hex IPV4: ", result)
40 local chunks = {ip:match("^(%d+)%.(%d+)%.(%d+)%.(%d+)$")}
42 for i,v in ipairs(chunks) do
43 if tonumber(v) > 255 then
44 print ("IPV4 ADDRESS ERROR: ", ip)
45 return "IPV4 ADDRESS ERROR"
47 address_chunks[#address_chunks + 1] = string.format ("%02x", v)
49 result = table.concat(address_chunks, " ")
50 print ("Hex IPV4: ", result)
55 for match in (ip..delimiter):gmatch("(.-)"..delimiter) do
57 number = tonumber(match, 16)
58 if number <= 65535 then
59 table.insert(address_chunks, string.format("%02x %02x",number/256,number % 256))
62 table.insert(address_chunks, "")
65 for i, chunk in ipairs(address_chunks) do
67 table.remove(address_chunks, i)
68 for j = 1,(8-#address_chunks) do
69 table.insert(address_chunks, i, "00 00")
74 result = table.concat(address_chunks, " ")
75 print ("Hex IPV6: ", result)