You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
4.7 KiB
150 lines
4.7 KiB
function parseRecord(t) |
|
local result = {} |
|
local start = 1 |
|
local type = hex_to_bytes(t, start, 1) |
|
--print("type:", type) |
|
start = start + 2 |
|
local version = hex_to_bytes(t, start, 1) |
|
--print("version:", version) |
|
start = start + 2 |
|
local time = hex_to_bytes(t, start, 8) |
|
--print("time:", time) |
|
start = start + 16 |
|
local pair = hex_to_bytes(t, start, 1) |
|
--print("pair:", pair) |
|
start = start + 2 |
|
local values = {} |
|
for i = 1, tonumber(pair, 16) do |
|
local address = hex_to_bytes(t, start, 2) |
|
-- print("startAdd:", address) |
|
start = start + 4 |
|
local size = hex_to_bytes(t, start, 2) |
|
--print("size:", size) |
|
start = start + 4 |
|
if tonumber(size, 16) / 2 == 0 |
|
then |
|
values[address] = "" |
|
end |
|
for j = 1, tonumber(size, 16) / 2 do |
|
--print(address, hex_to_bytes(t, start, 2)) |
|
values[address] = hex_to_bytes(t, start, 2) |
|
address = string.gsub(string.format("%04x", tonumber(address, 16) + 1), "0x", "") |
|
start = start + 4 |
|
end |
|
end |
|
result["type"] = type |
|
result["version"] = version |
|
result["time"] = time |
|
result["pair"] = pair |
|
result["values"] = values |
|
-- print(result["type"], result["version"], result["time"], result["pair"]) |
|
for k,v in pairsByKeys(result["values"]) do |
|
-- print(k,v) |
|
end |
|
-- print(tableToJson(result)) |
|
return tableToJson(result) |
|
end |
|
|
|
function pairsByKeys(t) |
|
local a = {} |
|
for n in pairs(t) do |
|
a[#a + 1] = n |
|
end |
|
table.sort(a) |
|
local i = 0 |
|
local iter = function() |
|
i = i + 1 |
|
if a[i] == nil |
|
then |
|
return nil |
|
else |
|
return a[i], t[a[i]] |
|
end |
|
end |
|
return iter |
|
end |
|
|
|
function length(t) |
|
local res=0 |
|
for k,v in pairs(t) do |
|
res=res+1 |
|
end |
|
return res |
|
end |
|
|
|
function hex_to_bytes(hex, start, len) |
|
local value = hex:sub(start, start + len * 2 - 1) |
|
if len > 1 |
|
then |
|
value = toBigEndian(value) |
|
end |
|
return value |
|
end |
|
|
|
function toBigEndian(value) |
|
--print(string.len(value)) |
|
local hexResult = "" |
|
for i = string.len(value), 1, -2 do |
|
local hex = value:sub(i - 1, i) |
|
hexResult = hexResult .. hex |
|
end |
|
return hexResult |
|
|
|
|
|
--local s2 = tonumber(value, 16) |
|
----print(s2) |
|
--s2 = string.pack("<i2", s2) |
|
--local hexResult = "" |
|
--for i = 1, #s2 do |
|
-- --local var = string.unpack("B", s2, i) |
|
-- hexResult = hexResult .. string.format("%x", string.unpack("B", s2, i)) |
|
-- --print(hexResult) |
|
--end |
|
--return hexResult |
|
end |
|
|
|
function tableToJson(tbl) |
|
local function serializeTable(val, name, indent, depth) |
|
local tblType = type(val) |
|
if tblType == "table" then |
|
local result = {} |
|
if depth > 0 then |
|
table.insert(result, "{\n") |
|
end |
|
local keys = {} |
|
for k in pairs(val) do |
|
table.insert(keys, k) |
|
end |
|
table.sort(keys) |
|
for i, k in ipairs(keys) do |
|
local v = val[k] |
|
local key = type(k) == "number" and "" .. k .. "" or '"' .. k .. '"' |
|
if type(v) == "table" then |
|
table.insert(result, string.rep(indent, depth + 1) .. key .. " : " .. serializeTable(v, name, indent, depth + 1) .. ",\n") |
|
else |
|
if(i == length(keys)) then |
|
table.insert(result, string.rep(indent, depth + 1) .. key .. " : \"" .. tostring(v) .. "\"\n") |
|
else |
|
table.insert(result, string.rep(indent, depth + 1) .. key .. " : \"" .. tostring(v) .. "\",\n") |
|
end |
|
end |
|
end |
|
if depth > 0 then |
|
table.insert(result, string.rep(indent, depth) .. "}\n") |
|
end |
|
return table.concat(result) |
|
elseif tblType == "number" then |
|
return tostring(val) |
|
elseif tblType == "string" then |
|
return '"' .. val .. '"' |
|
else |
|
return tostring(val) |
|
end |
|
end |
|
|
|
return "{" .. serializeTable(tbl, "root", "", 0) .. "}" |
|
end |
|
|
|
--parse("1Bfjx615uzpncE5GQsHydLQ631y2DmcWRQdnK4XqErRxyo2QSP88cmbhuKbUSnqFixv39kyBXpEewZJ8Uxb8yWVemoCQjQFFta9sxbm64NXYt8yNFB9QgyCLxnHdqcXF5hshzuY9VkGYpWxykgdr8c2TZ1XsjX7bDS35f6YfCfoU4qmQkTCgcZXNMLt7G7dL4aD8szZUHLwVn") |
|
-- parse("0001801a860f000000000451c30a0051c352c353c354c355c3419c0600419c429c439ca49c4400a49ca59ca69ca79ca89ca99caa9cab9cac9cad9cae9caf9cb09cb19cb29cb39cb49cb59cb69cb79cb89cb99cba9cbb9cbc9cbd9cbe9cbf9cc09cc19cc29cc39cc49cc59c317528003175327533753475357536753775387539753a753b753c753d753e753f7540754175427543754475") |
|
--toBigEndian("801a860f00000000") |