LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to swap bytes in LabVIEW?

Hello,

 

I was wondering if I could get some help on swapping bytes in LabVIEW? The data I'm reading in is a 3 byte integer in binary format with the most significant byte first, and I want to swap the LSB with the MSB. I'm thinking of extracting the bytes using the AND operators and swap the bytes, but that sounds inefficient Smiley Tongue .   Is there a VI that's similar to swapping 2 bytes?

 

Thanks in advance.

0 Kudos
Message 1 of 5
(4,910 Views)

LabVIEW does not have a 24-bit number, so when you get your data, you can convert it to a 32-bit number, and then to an array of Bytes which will have 4 elements. Get rid of the first Byte and you will have your 3-byte array. Then you can reverse the array order to swap the 2 bytes on the ends.

 

snippet.png

0 Kudos
Message 2 of 5
(4,895 Views)

LabVIEW also has direct functions for doing byte and word swaps (as well as splits and joins).  You can find them under the palette Numeric-->Data Manipulation.

 

Another thing that may be useful there is the pair of functions "Flatten to String" and "Unflatten from String".  They can also be used to reverse byte order by setting endian-ness.  

 

Below is a quick illustration of both:

 

 

-Kevin P

 

byte swap.pngbyte swap fp.png

 

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
Message 3 of 5
(4,881 Views)

Of course you can use a rotate or shift (after the swaps or unflatten) to go from 80402000 to 00804020 if that's what you want.

Message 4 of 5
(4,853 Views)

@SegFault1119 wrote:
The data I'm reading in is a 3 byte integer in binary format with the most significant byte first

Could you be more specific about how you are storing this data?  An example VI would help us help you.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 5 of 5
(4,826 Views)