From Saturday, Nov 23rd 7:00 PM CST - Sunday, Nov 24th 7:45 AM CST, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

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
(5,413 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
(5,398 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

 

ALERT! LabVIEW's subscription-only policy coming to an end (finally!). Permanent license pricing remains WIP. Tread carefully.
Message 3 of 5
(5,384 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
(5,356 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
(5,329 Views)