Academic Company Events Community Support Solutions Products & Services Contact NI MyNI
62 ratings:
 3.90 out of 5   

Writing Binary Files with LabVIEW That Can Be Read by Other Applications

Primary Software: LabVIEW Development Systems>>LabVIEW Full Development System
Primary Software Version: 7.1
Primary Software Fixed Version: N/A
Secondary Software: LabVIEW Development Systems>>LabVIEW Base Development System, LabVIEW Development Systems>>LabVIEW Professional Development System

I am using LabVIEW for Windows. When I write binary data to a file from LabVIEW and then try to read it back with a program written in another language, my data does not appear to be correct. I have already checked, and the program reading the data is reading the correct bytes for each field. How do I get LabVIEW to write a binary file that can be read in a C (or another language) program on Windows platforms?

The problem is that LabVIEW uses Big Endian format for its binary numbers, while most Windows programs use Little Endian format. LabVIEW uses Big Endian, because that is what the MacOS uses, and LabVIEW was created first for the Macintosh.

In Big Endian format, the most significant byte (MSB) of a multi-byte number is written first, then the second MSB, and so on down to the least significant byte (LSB). However in Little Endian, it is reversed such that the LSB is written first, then the second LSB on up to the MSB.

For example:
    a U8 "1" a U16 "1" a SGL precision Number
LabVIEW: "BIG ENDIAN" (00000001) (00000000 00000001) (Byte4:Byte3:Byte2:Byte1)
(Win) C: "LITTLE ENDIAN" (00000001) (00000001 00000000) (Byte1:Byte2:Byte3:Byte4)

In LabVIEW 8.0 and later, the Write Binary File VI and Read from Binary File VI have an input to specify the byte order. The byte order can also be specified when using Flatten to String VI and Unflatten from String VI.

Byte order input values of these VIs, indicating the endian format of the data:
  • 0 big-endian, network order (default)—The most-significant byte occupies the lowest memory address.

  • 1 native, host order—Uses the byte-ordering format of the host computer.

  • 2 little-endian—The least-significant byte occupies the lowest memory address.

For an example on how to write a file using proper "Endian" representation, check out Write Binary in the Example Finder. Select Help»Find Examples. Then navigate to Fundamentals»File Input and Output»Write Binary

To read a little endian binary file in LabVIEW 7.1, check out the Developer Zone Example Program: Reading Little Endian Binary Files with LabVIEW 7.1.

LabWindows/CVI has formatting and scanning functions that can specify byte order. The format modifier is [o0123], [o01] for 32 and 16 bit Intel, and [o3210] and [o10] for Motorola byte order. This is another way to interpret them.

Related Links:
Developer Zone Example Program: Reading Little Endian Binary Files with LabVIEW 7.1


Report Date: 08/31/1997
Last Updated: 11/20/2014
Document ID: 10U39NAQ

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
  1 2 3 4 5
Please Contact NI for all product and support inquiries.submit