From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Style suggestions for bit array to byte array

Solved!
Go to solution

I have a spec that defines message data as an array of bytes, and each byte has specific bits that represent different things. So, I just build up an array of booleans representing the message bits, then type cast to a u8 array with convert 4.x selected. But, the type cast function documentation states "National Instruments recommends reworking any application that uses the Convert 4.x Data mode as a long term solution." Is there a performance reason for this? I can think of other methods to eliminate the 4.x cast, but if I go another route I'd like to be able to justify the reason. Thanks.

0 Kudos
Message 1 of 10
(5,918 Views)

I'm guessing it's not about performance, but rather about compatibility. Relying on something that was replaced almost 13 years ago does not seem too wise. I would suggest the Boolean Array to Number primitive.


___________________
Try to take over the world!
Message 2 of 10
(5,909 Views)

@tst wrote:

I'm guessing it's not about performance, but rather about compatibility. Relying on something that was replaced almost 13 years ago does not seem too wise. I would suggest the Boolean Array to Number primitive.


That's fine, is the best way then to build multiple, 8-boolean arrays, convert each one to u8 numbers then build the u8 array out of those?

0 Kudos
Message 3 of 10
(5,903 Views)
Solution
Accepted by topic author GregFreeman

Something like this is likely what you are trying to do.


GCentral
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
Message 4 of 10
(5,895 Views)

yesuh, it is.

0 Kudos
Message 5 of 10
(5,886 Views)

I am trying to console Type Cast man at the moment as he sees the length you go to avoid him.

 

If more people use 4.x mode then it will not go away anytime soon.  It is built for this exact purpose, converting human readable (MSB first) boolean arrays to byte arrays.  And if it does go away, then no doubt a replacement primitive for this operation will be provided.  Many VIs in vi.lib use it.

 

Assuming your array is fixed size, then why not simply set the output representation of the BA to Number primitive to the suitable size (U8-U64) and simply type cast the result to a byte array (no need for 4.x mode).  If you have an odd number of bytes simply delete the final element.  If you have a user punching in more than 64 bits using a boolean array, I think the 65th thing being punched is your face.

 

 

Message 6 of 10
(5,861 Views)

@Darin.K wrote:

  If you have a user punching in more than 64 bits using a boolean array, I think the 65th thing being punched is your face.

 

 


This is what I'm worried about. I'd like things to be consistent across all functions that will be building message data. And I may get a definition for a message with more than 8 bytes of data. This is not data a user is entering, it's data being sent across the network. Doubles are being converted to a specified fixed-point number with x number of fractional bits and y number of word bits. It's good to know 4.x does msb first. I was just trying to type cast a bit array to a u8 array, but because booleans are stored as bytes and not bits, it wasn't working properly. I noticed casting using 4.x treated the boolean array as bits. It sounds like I may need to be sure to properly reverse my array.

0 Kudos
Message 7 of 10
(5,856 Views)

Kudos, also Darin. I didn't know you could configure the boolean array to number output!

0 Kudos
Message 8 of 10
(5,841 Views)

@for(imstuck) wrote:

Kudos, also Darin. I didn't know you could configure the boolean array to number output!


You didn't noticed I did that in my snippet (configured to output a U8)?


GCentral
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 9 of 10
(5,812 Views)

@crossrulz wrote:

@for(imstuck) wrote:

Kudos, also Darin. I didn't know you could configure the boolean array to number output!


You didn't noticed I did that in my snippet (configured to output a U8)?


I glanced at your snippet, but didn't study in detail or drag it to my bd because it was easy enough to understand visually. I would have run into it once I implement it in code

0 Kudos
Message 10 of 10
(5,797 Views)