From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Example Code

Changing the Endian Form of Binary Numeric Data

Code and Documents

Attachment

Overview


This example demonstrates how to convert binary numeric data to an endian form that an application not created with LabVIEW can read.

LabVIEW stores numbers in big-endian form, with the most-significant byte first in memory. Mac and Sun applications usually expect the data to be of big-endian form, but Windows applications usually expect little-endian form, with the least-significant byte first. As a result, for a Windows application that requires little-endian form, you must explicitly swap the bytes in each number after you flatten or before you unflatten the data. If a Windows application requires little-endian two-byte binary data, you must swap the first byte with the second byte in each number before you write the data. 

The example VI (attached) shows five different methods for converting data from big-endian to little-endian form, or vice versa.

Note  Floating-point numbers in LabVIEW are IEEE-compatible, and the target application must be able to read such numbers. Single- and double-precision floating-point numbers are typically formatted appropriately, but extended-precision numbers, for which there are several formats in use, might not be.

Refer to the Application Note on LabVIEW Data Storage (linked below) for more information about endian forms.

LabVIEW 7.1 and Earlier

When you change the endian form of data, do not use the option of the LabVIEW Write File function that automatically writes a file header. This header is always of big-endian form. Instead, if you need a size header, write it explicitly before you write data to the file.

You must know the format the target application expects. For example, while an application might recognize a file created with the Write to I16 File or Write to SGL File functions with or without byte swapping, it might expect a type of header that these VIs do not supply. When you know exactly which format the application expects, use the appropriate features in LabVIEW to create such a file.

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Contributors