'Trying to pack binary bits represented by bytes into a byte using String.pack with Hex escaped Sequence(i.e. \x equivalent) format in Lua
I want to to pack binary bits represented by bytes into a byte using String.pack with Hex escaped Sequence(i.e. \x equivalent) format in Lua.But I don't Know How.Infact I have a function in python that do this. and I want to do this in Lua Too But I have problem Function in Lua:
MESSAGE_HEADER_FMT = ">4I"
PREFIX_VALUE = 0x000055AA
def pack_message_test():
buffer = (
struct.pack(
MESSAGE_HEADER_FMT,
PREFIX_VALUE,
2,
7,
121,
)
)
return buffer
print(pack_message_test())
The ouput of python function is:
b'\x00\x00U\xaa\x00\x00\x00\x02\x00\x00\x00\x07\x00\x00\x00y'
then I want do the same thing in Lua Language. so I write this Function in Lua:
function Pack:create(fmt,ps_value,body_len)
local pack = {}
local pattern = fmt
pack.cmd = 7
pack.seqno = 2
pack.prefix_value = ps_value
pack.body_len = body_len
function pack:split()
return self.prefix_value,self.seqno,self.cmd,body_len
end
function pack:hex()
return string.pack(pattern,self:split())
end
return pack
end
pack = Pack:create(">I4",0x000055AA,121)
hex_packet = pack:hex()
The ouput of Lua function is:
U¬
I want to give the same output as python function in Lua function. what is the problem and how fix it?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
