Extra Bytes in Binary File Containing LabVIEW Array

Updated Sep 14, 2023

Reported In

Software

  • LabVIEW

Issue Details

  • When I write to a binary file in LabVIEW, the data in the binary file is correct if I write scalar values but it does not show up correctly if I write arrays of the same data type.  The data is correct when it is written to and read from LabVIEW.  However, the problem shows up when the binary file is read using another program besides LabVIEW.
  • If I open the binary file I wrote to in LabVIEW in a binary file viewer, another program, or another programming language, there are extra bytes in the binary file.  I did a calculation of the bytes in the file and regardless of the size of my array or data type (I8, U8, I16, U16, I32, U32, DBL, SGL, FXP, Boolean), there is always a consistent extra 4 bytes (32 bits) or a multiple of 4 bytes in my binary file.  I am not sure where the extra bytes are coming from and how to read my data from the binary file correctly outside of LabVIEW.
  • I wrote a 2 dimensional (2d) array of 8-bit integers using the Write to Binary File function in LabVIEW and I see 8 extra bytes in the beginning of my binary file as shown in the highlighted portion in the image below.  How can I get rid of those extra bytes of data? 

Solution

When you write an array in LabVIEW to a binary file using the Write to Binary File node (Programming >> File I/O >> Write to Binary File), the size of each dimension of your array is written to the beginning of the binary file by default.  You have three options to work with binary files written in LabVIEW that need to be read outside of LabVIEW:
 
  • Set prepend array or string size? to FALSE

The Write to Binary File Function has the prepend array or string size? parameter that is TRUE by default.  If you set this parameter to FALSE, the binary file will not include data size information at the beginning of the binary file. 
Note: This parameter only controls the top-level data size information.  Arrays and Strings in hierarchical data types such as clusters will always include the size information.  As such, if you are writing hierarchical data types to a binary file, setting this parameter to FALSE will not get rid of all the extra bytes you are seeing. 
 
  • Identify Location of Extra Bytes and Work With Them

If you decide to keep the prepend array or string size? parameter TRUE or if you are working with hierarchical data that will always have size information retained to a certain extent, the best way to work with the binary data is to understand where the extra bytes are located and programmatically or manually handle those extra bytes.
 
  • Write Your Data Using a Different Data Structure

If your data contains hierarchical data that makes it such that size data is written to the binary file even after you set the prepend array or string size? parameter to FALSE, you can choose to change how you structure the data you are writing to the binary file.  Because binary files really do not have a defined structure, it will be up to you to determine how you want to organize your data to be written out.  The most important part when doing this is to make sure the other programs you are using to read in the binary files are also using the same data organization scheme.

Additional Information

Different data types in LabVIEW are stored differently in memory and this is also reflected in how these data types are written to a binary file.