Ask Your Question

How can I read a Buffer in a Little Endian order?

asked 2021-08-05 08:02:52 +0000

lluc_fn gravatar image

updated 2021-08-05 08:08:14 +0000

In LUA, wow could I get the number (integer, float,...) from the following bytes -buffer(94,2)- in LITTLE ENDIAN? Afterwords I want to multiply this number by a constant "x".

What I did:

-- First of all, creation of the protofield

somefield = ProtoField.uint16("foo.somefield", "Some Field", base.DEC, nil, 0xffff)

number = tonumber(tostring(buffer(94,2))) -- I get here the BIG ENDIAN number but I want the LITTLE ENDIAN one

x=2 -- value of the constant

number2 = number*x


As I said, with this code I get the BIG ENDIAN order from "number" and I want to get the LITTLE ENDIAN one and store it in "number". I would appreciate a lot if you could help me. Many thanks for your help!

edit retag flag offensive close merge delete


1 Answer

Sort by ยป oldest newest most voted

answered 2021-08-05 16:03:22 +0000

cmaynard gravatar image

As I mentioned in the comment to this question, the answer is:

subtree:add_le(somefield, buffer(94, 2), buffer(94, 2):le_uint() * x)

NOTE: Because you're specifying the value to use, it doesn't make any difference if you use subtree:add() or subtree:add_le(), but if your protocol is Little-Endian, then it's better to be consistent and use subtree:add_le().

Also, since you're using the entire value, there's no need to specify a mask; you can just use:

somefield = ProtoField.uint16("foo.somefield", "Some Field", base.DEC)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2021-08-05 08:02:52 +0000

Seen: 1,026 times

Last updated: Aug 05 '21