LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Converting elements in an array row to 32bit binary number

Hello, I hope someone can help me fit together the pieces of this puzzle.  I've used some of the posts already on the forum to get to the stage I'm at now.  My problem is this:

I want to write a 1D array of 8192 rows each containing a 64bit number to a binary file (65536bytes size).  I have worked out how to write two 32bit numbers as one 64bit one with a cluster using a previous post.  So my problem is now in handling the 32bit numbers. 

What I have started by using is a 2D array of 8192 rows x 64 columns, each element of which contains a 0 or 1 so I can control the value of each bit of the 64 bit number individually.  However, for each line of the array I need to join the first 32 independent elements together to make one 32bit number (and then another with the last 32 elements)
e.g. (array elements separated by commas) array row 1,1,0,0,1,0,1,0 => 11001010 number in binary representation (for 8 bits)

I have considered a number of ways of doing this (using spreadsheet strings/boolean/number) but none seems to convert effectively between my start and end point.  I have no preference of which data type to use provided I can manipulate each element of the 64bit (or 2x32bit) number independently and output a file of the correct size (see above).

I'd be grateful for any suggestions I could try out!  I will be checking the board regularly so please post if you need any further information.

Many thanks.

Sarah
0 Kudos
Message 1 of 10
(5,355 Views)
Hello Sarah,
you can try the function "flatten to string" in the palette "data manipulation". It makes a string from any data you wire to the input. If you pass your cluster of two U32 (or I32, however) to this function, you get a String with the length 8, i.e. the 2 x 4 bytes of your two U32-numbers. Then this String you can write to the file.
Consider that LabView uses big-endian-order of saving data in memory, this means first comes the highest byte of the first U32 in your cluster. You can change the order with the functions "swap bytes / words" in the same palette.
Hope this helps,
Dave
Greets, Dave
0 Kudos
Message 2 of 10
(5,349 Views)

Hi Dave,

My file writes ok from the 2 32bit numbers using the typecast function with the cluster as an input.  The problem I am having is more getting the two numbers in the first place.  I need to manipulate each bit individuall so I naturally started using an array but now I can't get the array elements to join together into the numbers.  I have tried using 'array to spreadsheet string' but then each value of 0 or 1 seems to be stored as a byte rather than a bit as the file size is very large.  I just want a normal string with the array values in or rather, a 32bit number where each bit is one of the array values.

Sarah
0 Kudos
Message 3 of 10
(5,347 Views)
well, I just realized that the "flatten to string"- function has improved (in LV8) since the last time I used it. Now you don't have to reorder the bytes, but I suppose you have to set the input "prepend array or string size?" to false.
I just had a glance into the LV-Help for data storage formats and found in the branch "Fundamentals/How LabView stores Data in Memory" the page you should read to understand, what Labview does to convert your cluster to a string.
Dave
Greets, Dave
0 Kudos
Message 4 of 10
(5,344 Views)
I hope that I understood your problem now: you need to use the function "boolean array to number", wich exactly does, what you want: it make an U32 - integer from an array of booleans. It's located in the "boolean"-palette.
Dave
Greets, Dave
Message 5 of 10
(5,341 Views)
Hi Dave,

Thats it exactly, I've got it all working now.  One more question - I notice there is a function to convert the boolean array to (0,1) in that palette - is there any option to convert the other way?  It would be useful for me to work with a numeric type rather than boolean if possible.

Thanks a lot for your help!

Sarah
0 Kudos
Message 6 of 10
(5,335 Views)
Yes, you just take the function "greater than zero", so you can test, whether the integer is "1", then it returns "True". Since the Input of this function is polymorphic, you can wire arrays, clusters, arrays of clusters and so on.
Dave
Greets, Dave
0 Kudos
Message 7 of 10
(5,329 Views)

Sarah,

It sounds like you need to use a boolean data type instead of an integer type.  The smallest size for a integer type is 8 bits as you found out.

Once you have an array of booleans you can use the "boolean array to number" to get an integer value of the boolean array as Dave points out.  To go the other way, use "Number To Boolean Array" to get an array of booleans from an integer type.  The largest size integer the "boolean array to number" will generate is 32 bit.

Otherwise you will have to logicaly shift each "bit" and use the OR operation to combine them.

Paul

0 Kudos
Message 8 of 10
(5,327 Views)
I'd actually recommend making a habit of using "not equal to 0" instead of "greater than 0" as the comparison.  Either is fine when you know for sure that the input data can only be 0 or 1 values, but there's a more general case where you don't know what the input values might be.  As far as I know, it'd be much more commonplace to consider all non-zero values as True.  You could think of the number-->boolean conversion as a kind of logical OR-ing of all the bits that represent that number.  If any bit is non-zero, the boolean value of the number is True.
 
-Kevin P.
ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
Message 9 of 10
(5,308 Views)
That's right, Kevin, these are the bugs, that it works correctly for years and then one day it fails and you need another years to find the bug.
Greets, Dave
0 Kudos
Message 10 of 10
(5,284 Views)