LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert an U8 array to a LabView data type

Solved!
Go to solution

In my code the input is an fixed length of U8 array reflecting data in memory, I need to convert it to a predefined data type. For example, the first 2 U8 elements in the arrary need to comvert to an U16 data, next 4 U8 elements to U32 data. I am just wondering if there is any easy way to do that kind of conversion. 

0 Kudos
Message 1 of 11
(13,622 Views)

It can be done like this:

 

Example.png

These functions are found here:

 

Example1.png

Tim
GHSP
0 Kudos
Message 2 of 11
(13,617 Views)

In the first case use the Join Numbers function to join the bytes. In the second case, use the Join Numbers function three times: twice to join the consecutive bytes and once more to join the words into a single long word.

 

Mike...


Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 3 of 11
(13,609 Views)

Create a cluster constant where the elements are of the proper type and in the right order, Then simpy typecast your array to that cluster

0 Kudos
Message 4 of 11
(13,606 Views)

Aeastet - that's not what the poster wants at all.

 

Scott - try a typecast to a cluster as shown here (as usual, the snippet tool messed up the cluster, but if you drag it to a block diagram it will work):

typecast U8 array.png

typecast U8 array FP.png

Message 5 of 11
(13,605 Views)
Solution
Accepted by topic author Scott-D

Type Cast Man and his trusty sidekick Unflatten From String are itching for some action.

 

ConvertNumbers.png

 

Use Unflatten from string if you have problems with endianness. 

Message 6 of 11
(13,599 Views)

I guess I didn't make myself clear.  I tried using Type cast to receved an U8 array and created a cluster ( contains U16 and U32 data) to define my needed data type. The output looks good, but U16 and U32 are byte-swapped. Sounds like to solve this is to manually swap U8 array before Type cast. But I wonder if there is any LabView function I can use to avoid manual swap by rewriing U8 array.

0 Kudos
Message 7 of 11
(13,598 Views)

If you use the unflatten route of Darin's second example, you can specify the byte order.

Message 8 of 11
(13,592 Views)

Unflatten from string is easy, but you can also wire an entire cluster to "Swap Bytes" and "Swap Words" and you'll get that same cluster with reversed endianness (Swap Words will ignore U16 values).

0 Kudos
Message 9 of 11
(13,589 Views)

It works!!!  thank you guys so much! I use Unflatten from string with "native, jost order" to solve the byte swap problem.

 

Have a great day!!

0 Kudos
Message 10 of 11
(13,579 Views)