LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert an array of bits into an array of bytes?

If I have an array of 1s and 0s, how can I pack them
into an array of bytes (8 bits)?
Thanks.
0 Kudos
Message 1 of 11
(6,265 Views)
Here's an easy solution: Convert your 0,1 array to a boolean array, then type cast it to a U8 array.
(see attached example).

Make sure to select 4.x behavior for the type cast function!
Message 2 of 11
(6,265 Views)
NI recommends to get rid of 4.x mode (Quote from the help file: "National Instruments recommends reworking any application that uses the Convert 4.x Data mode as a long term solution."). For this reason, I have attached an alternative example to give you some ideas.

(The previous example (using 4.x type cast) is actually slightly flawed, because for some reason it only produces even sized output arrays (why???) so you would need to trim the resulting array as a function of the input array size.)
Message 3 of 11
(6,265 Views)
Thank you Altenbach.
Can I ask you to save
this in a 6.1 version?
I don't have version 7.0.

Thank you,
Van
0 Kudos
Message 4 of 11
(6,265 Views)
Here's the same in LabVIEW 6.1.
0 Kudos
Message 5 of 11
(6,265 Views)
For your reference, here's an image of the code (since you don't have LabVIEW 7.0).
0 Kudos
Message 6 of 11
(6,265 Views)
Thank you Altenbach.
You're a life saver.

Van
0 Kudos
Message 7 of 11
(6,265 Views)
Thanks very much Altenbach.

Van
0 Kudos
Message 8 of 11
(6,265 Views)
altenbach wrote:

> (The previous example (using 4.x type cast) is actually slightly
> flawed, because for some reason it only produces even sized output
> arrays (why???) so you would need to trim the resulting array as a
> function of the input array size.)

LabVIEW 4 booleans were 16 bit integers with the most significant bit
set to indicate 1 and cleared to indicate 0.

Boolean arrays were bit packed, whereas in LabVIEW >= 5.0 they are just
arrays of 8 bit values with only the least significant bit used. LabVIEW
4 behaviour was to pad any boolean array to 16 bits as 16 bit integers
was the base type of skalar booleans.

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 9 of 11
(6,265 Views)
Thank you Rolf.

Van
0 Kudos
Message 10 of 11
(6,265 Views)