01-18-2023 06:10 AM
Hi all,
LV Newb here, but been messing around with programming languages since the 90s. Finding it really hard to get my head around LV, which is frustrating, and I can't find things that I expect should be there. I have spent ages looking through Google and YouTube and am annoyed enough at the lack of answers that I have signed up just to ask a question. Hopefully one of you can help with what should be a really simple answer.
I have an array of U8, I want to subset it and flatten (e.g. I have [1,1,0,0,1,0,1,0] and I want the top 4, I want to subset 0:3 and get out "1100"). I have done stuff like this over the years in various languages so know it is possible. Is this not easily achieved in LV with a function? I have tried various things to get it to work without success.
Any advice is appreciated, thank you.
LVN
Solved! Go to Solution.
01-18-2023 06:31 AM
01-18-2023 06:37 AM
Hi,
Thanks for your reply, and apologies that I didn't make it more clear in my original question.
I am already using Array subset, but then getting an array. Continuing my example, I get [1,1,0,0]. I can't find a way to get out "1100", either directly or any kind of data conversion. I have tried "flatten to string", "type cast", "byte array to string" etc. without any luck.
Regards,
LVN
01-18-2023 07:11 AM
Hi newb,
@lv_newb wrote:
I am already using Array subset, but then getting an array. Continuing my example, I get [1,1,0,0]. I can't find a way to get out "1100", either directly or any kind of data conversion. I have tried "flatten to string", "type cast", "byte array to string" etc. without any luck.
The typical way to calculate a numeric value from a bunch of bits is this:
x = sum( bit(i) * 2^i) (for all bits in your array)
Use an autoindexing loop to access the array elements…
The more LabVIEW-style approach is to use the BooleanArrayToNum function!
01-18-2023 07:12 AM - edited 01-18-2023 07:13 AM
One of various possibilities (if you want a string, it's not that clear...):
01-18-2023 07:48 AM
Thanks GerdW,
OK, so it sounds like there is no built-in one-stop function to extract the values. In which case I will do as you suggest, create a subset then loop through, indexing each element and concantenating them together.
For my purposes I need to retain them in bit representation (e.g. 1100 as opposed to its decimal numerical equivalent).
Thank you for the advice.
LVN
01-18-2023 07:57 AM
oh wow, pincpanter, that is exactly what I wanted! I just never thought I would have to have some many intermediary steps!
01-18-2023 08:04 AM - edited 01-18-2023 08:05 AM
Comment deleted.
01-18-2023 08:34 AM
Hi newb,
@lv_newb wrote:
I just never thought I would have to have some many intermediary steps!
That are just 3 steps…
@lv_newb wrote:
I have an array of U8, I want to subset it and flatten (e.g. I have [1,1,0,0,1,0,1,0] and I want the top 4, I want to subset 0:3 and get out "1100").
You want to
Each step is done with just one function…
@lv_newb wrote:
I have [1,1,0,0,1,0,1,0] and I want the top 4, I want to subset 0:3
You really need to learn to write your questions more accurate!
General suggestion:
Always attach a VI with your input data stored in a control and with an indicator showing the expected output result. This always helps to determine the expected/required input and output datatypes!
01-18-2023 10:06 AM - edited 01-18-2023 10:26 AM
@lv_newb wrote:
I have an array of U8, I want to subset it and flatten (e.g. I have [1,1,0,0,1,0,1,0] and I want the top 4, I want to subset 0:3 and get out "1100"). I have done stuff like this over the years in various languages so know it is possible. Is this not easily achieved in LV with a function?
So what exactly is the end result you want? Which programming language would be able to do "stuff like this" in fewer steps?
In LabVIEW, you would wrap the solution into a SubVI for reuse. (U8 array input, String output) One single step in all future use!
So why exactly do you have an array of U8 instead of a singe U8 scalar that would be sufficient to represent 8 bits? Where does the data come from? Is the element at index 0 the LSB or the MSB? Typically the first element is LSB, so the result would be 0011, right?
Why do you want a string instead of a number in the end?
Can we please take a step back and describe the bigger context. How was the data before it became an U8 array of zeroes and ones? What should happen with the result?
Overall, you start with 8 bits of information inflated to 96bits (8x U8 + 32bit size header) and you want to covert half of it it to a string of ASCII characters (64bits, 42bit string data and 32bit size header). Seems Convoluted!
Here are some other hints: