01-12-2015 10:48 AM
Is there a better way to do this in LabVIEW without converting it to string first? I got a number that I want to convert to 4 byte array. And also convert that number to 2 byte array.
I have attached VI of how I am currently handling it. This works fine but I think there probably a better method.
Thx in advance
01-12-2015 11:00 AM - edited 01-12-2015 11:01 AM
What are you really trying to do? Give us an example of a number you have and what you want the result to be.
You are taking a floating point number and converting it to an integer. Is that what you want?
With what you have, you can eliminate the string to byte array if you wire a U8 integer constant placed into an array constant into the top of the typecast function.
01-12-2015 12:14 PM
If your byte array is going anywhere other than somewhere else on the same Block Diagram I would consider using 'Flatten to String' instead of Type Cast in this situation. It gives you explicit control over the endianness of the result when you need it.
01-14-2015 02:57 PM
@ ravenfan,
I initialize an array that has 100 u8 elements. Using a shift register I past that array to every state in my state machine. Depending on the state I will be using double, Boolean, and unsigned byte.
Several items in the array represent two byte numbers. While other represent 4 byte number.
I have attached an picture. Thx for suggestion
@ Darin,
I am not sure how to use flatten to string. Can you provide example?
01-14-2015 03:14 PM - edited 01-14-2015 03:17 PM
You need to attach the actual VI. There is no way to tell any details from a picture (datatypes, etc).
Instead of that gigantic IPE structure, a simple "replace array subset" would seem sufficient. Also most of the code seem overly complicated. I would use built array to create the U16 array, cast it to an U8 array, and replace the relevant portion of the original array with it in one step.
01-14-2015 03:26 PM - edited 01-14-2015 03:28 PM
Is there a reason you need to pass data as a byte array rather than just using it as a cluster throughout your VI?
I would get rid of the in place element structure, the indexing is making the code heavier than it should need to be. For a 100 element array, that is not much memory that you should not have to worry about not making copies and doing everything in place. I would use Build Array with concatenate elements set. Then use Replace array subset to put it into your array. That would elliminate the IEP and all of the extra Index Arrays you have to break it into individual elements.
Here is an example of how flatten to string would work with a cluster (and the corresponding string output in hex display). But I don't think you would use this in your case. You are taking some of your data such as double floating point, coercing it to a U16. This takes any number that has a fraction part and rounds it down. Is that what you truly want to do? It could also be truncating any data that would exceed the range of a U16 (0 ti 65535) The binary representation of an integer as a U16 is completely different than the binary representation of that same number as a double.
01-15-2015 07:53 AM
You could also just use a variant to pass the data around.