Writing Binary Files with LabVIEW That Can Be Read by Other Applications Primary Software: LabVIEW Development Systems>>Full Development SystemPrimary 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:
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:
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:
Report Date: 08/31/1997 Last Updated: 02/08/2007 Document ID: 10U39NAQ |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
