Skip to content

Instantly share code, notes, and snippets.

@leandromoreira
Created November 16, 2020 22:31
Show Gist options
  • Save leandromoreira/dcd9b653dfdb7d95eb03d42cbf8ad8a9 to your computer and use it in GitHub Desktop.
Save leandromoreira/dcd9b653dfdb7d95eb03d42cbf8ad8a9 to your computer and use it in GitHub Desktop.
val = "a0192faa0"
function string.pack(str)
local packed_string = "" for i=1, #str, 2 do
local hex_number = tonumber("0x" .. str:sub(i,i) .. str:sub(i+1,i+1))
packed_string = packed_string .. string.char(hex_number)
end return packed_string
end
print("==========")
print(val)
print("==========")
binary_string = string.pack(val)
print(binary_string, type(binary_string), #binary_string)
print("==========")
for i=1, #binary_string do
print(string.byte(binary_string, i), string.format("%x", string.byte(binary_string, i)))
end
print("==========")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment