Ask Your Question

How to get 20 of 24 bits in a LITTLE-ENDIAN Coding?

asked 2021-08-02 14:43:36 +0000

lluc_fn gravatar image

updated 2021-08-02 16:24:59 +0000

cmaynard gravatar image

I am writing my LUA-Code in order to decode a UDP-Payload. I need to read JUST the first 20 bits of the following 3 bytes in a LITTLE-ENDIAN decoding: I tried with the following function: ::subtree:add_le((buffer:range(79, 3):bitfield(0, 20)):le_uint()) But I get this error:

::Lua Error: ...oftware\WiresharkPortable\App\Wireshark\diagnose_118.lua:94: attempt to index a number value

The bytes seen in the payload of the UDP-Message may be the following:

BIG-ENDIAN -->FF 35 50
The hexadecimal-data interesting for me is --> 0 35 FF --> 0000 0011 0101 1111 1111

The problem appears when I introduce the function bitfield - I cannot avoid it as I just want the first 20 bits and not the whole 24. Do you know another different way to fulfill my purpose? Many thanks in advance! :)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2021-08-02 16:29:47 +0000

cmaynard gravatar image

First, it doesn't look like you're calling subtree:add_le() correctly, because the first argument should be a protofield, but you're passing it a tvbrange.

Well, assuming you have a protofield defined as follows:

somefield = ProtoField.uint24("foo.somefield", "Some Field", base.DEC, nil, 0x0fffff)

Then you should just need to add it to the tree using:

subtree:add_le(somefield, buffer(79, 3))
edit flag offensive delete link more


Many thanks for your help, it worked! Now I got the following question. How 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:

number = tonumber(tostring(buffer(94,2))) -- I get here the BIG ENDIAN number and 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 again for your help!

lluc_fn gravatar imagelluc_fn ( 2021-08-04 08:59:34 +0000 )edit

In that case, you should be able to use something like this:

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

NOTE: Don't keep using the same question to ask more questions. Open a new question for each question you have.

cmaynard gravatar imagecmaynard ( 2021-08-04 19:56:26 +0000 )edit

Your Answer

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

Add Answer

Question Tools


Asked: 2021-08-02 14:43:36 +0000

Seen: 64 times

Last updated: Aug 02