Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI
45 ratings:
 4.20 out of 5     Rate this Document

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

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

Problem: 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?

Solution: 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.

In LabVIEW 7.1 and earlier, there are several ways to convert between LabVIEW and C format binary files. For an example on how to write a file using proper "Endian" representation, check out the link given below in the Related Links field.

The two VIs attached below convert files from Big Endian to Little Endian (and vice versa) either for reading into LabVIEW or for writing out of LabVIEW -- and they can do so for every LabVIEW data type (except complex and extended). The data files to test this are also included in a zip format below -- two of each of the 8 data types, one made by LabVIEW, one made by Microsoft Visual C++. When you select the "Byte Swap" button on the VI, you can interpret the C files just as if they were LabVIEW files and write data files that C can read directly. Each data file contains the numbers 1 through 10 in that particular data representation.

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.

NOTE: Sun computers are also Big Endian machines.

Related Links: KnowledgeBase 1XU8RQPH: What Does the Term "Endian" Mean?


Attachments:


bin_file_write.vibin_file_read.vibin_data_files.zipbin_C_proj.zip


Report Date: 08/31/1997
Last Updated: 02/08/2007
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