LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Extracting varying number of bits from byte array

Solved!
Go to solution

Hi,

 

I was working on decoding a binary file and one of the task is to extract out the data of varying bits from a U8 Array of fixed length. The image below is a simple illustration of this particular task.

Picture1.png

Now, before posting you solution, I've already considered 2 methods from the forum that I've searched so far, here is the following snippet of a simple example code.

1.png

So from 2 array element of U8 data, I'm extracting out 3-, 9- and 6-bits of data from that array and place it in another array (for simplicity).

 

However, I believe that these 2 methods may not be efficient in terms of speed and memory performance.

I would like to know if there are other alternatives for solve this problem. If so, do share with us.

 

Thanks.

 

Regards,

Ong Ee Lim



See that button on the left side of this post...

If you feel my post is helpful, all you need is just (at most) 2 seconds to click that button, to show your appreciation. Thank you~~



0 Kudos
Message 1 of 8
(6,872 Views)

Hi Ong,

 

you could convert/typecast your U8 array to U64 values (if you are dealing with 8 byte long blocks).

Then apply simple math using AND and SHIFT to get your values from the bitfield…

 

Are you working with CAN data?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 8
(6,861 Views)

ongeelim wrote:

However, I believe that these 2 methods may not be efficient in terms of speed and memory performance.


Why? Have you measured it? Do you have a lot of data or limited time?


___________________
Try to take over the world!
0 Kudos
Message 3 of 8
(6,848 Views)
Solution
Accepted by topic author ongeelim

I'm with GerdW here.  If you have a set block of data (8 or less blocks), read the data as a U64 and then do some bit shifting and masking with ANDs to get your data.

 

But looking at your two methods, you could not use the Delete From Array since that is an expensive process.  Instead, use Array Subset and keep track of where you are in the array with another shift register.  That would avoid some memory allocations and speed up Method 2.



There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 4 of 8
(6,840 Views)

What you really need is a bit field data type like we have in C.

 

So click on this link and vote (Kudos) for this LabVIEW Ideal Exchange

 

Provide the ability to use bit fields in clusters

Omar
0 Kudos
Message 5 of 8
(6,822 Views)

Hi GerdW,

 

Thank you for the recommendation.

I'm not working with CAN data, but rather working on the codes/functions to read a specific file type (similar to LabVIEW built-in functions to read JPEG, GIF, WAV, etc) for personal interest).

This file type will have lots of frames (Depending on the file size of the file that any user will specify).

And each frame, there will be a data block that I need to extract from which would be either 17 or 32 bytes long (Sorry if I didn't mentioned about the size of the block that I'll need to extract), depending on the situation.

 

tst,

 

Have you measured it?

I'll do the benchmarking on the code analyzing one frame later on.

Do you have a lot of data or limited time?

I would say both.

Limited time as there may be heavier computations to be done for each frame later on.

Lot of data as the initial plan is to read the whole data from a file and then analyze, rather than streaming data from disk. File size will vary depending on the file size of the file that any user will specify.



See that button on the left side of this post...

If you feel my post is helpful, all you need is just (at most) 2 seconds to click that button, to show your appreciation. Thank you~~



0 Kudos
Message 6 of 8
(6,803 Views)

Yeah avoid method 1, but method 2 doesn't look terrible.  There are some improvements like crossrulz mentioned, but given the task there are many worst ways to come to the same result.

0 Kudos
Message 7 of 8
(6,781 Views)

Hi,

 

Thanks for the reply, in that case, I'll just stick to the modified method 2, as described by crossrulz. Will run the benchmarking test once I get the code to analyze one frame done.

 

Thanks.

 

Regards,

Ong Ee Lim



See that button on the left side of this post...

If you feel my post is helpful, all you need is just (at most) 2 seconds to click that button, to show your appreciation. Thank you~~



0 Kudos
Message 8 of 8
(6,758 Views)