First time here? Check out the FAQ!
THIS IS A TEST INSTANCE. Feel free to ask and answer questions, but take care to avoid triggering too many notifications.
0

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

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

subtree:add(somefield,number2)

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!

lluc_fn's avatar
3
lluc_fn
asked 2021-08-05 08:02:52 +0000, updated 2021-08-05 08:08:14 +0000
edit flag offensive 0 remove flag close merge delete

Comments

Chuckc's avatar Chuckc (2021-08-05 15:35:26 +0000) edit
add a comment see more comments

1 Answer

0

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)
cmaynard's avatar
11.1k
cmaynard
answered 2021-08-05 16:03:22 +0000
edit flag offensive 0 remove flag delete link

Comments

add a comment see more comments

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss.

Add Answer