Binary Data Files
A binary file is typically much more compact than an ASCII
file for raw data storage. The reason is simply that an unsigned 16-bit
value in ASCII might look like 48373
,
which takes up five characters, but in binary it requires only two bytes
of storage, which in hexadecimal would be written as BC F5
. A file with 16K
(214 entries) worth of unique data values in
ASCII could end up being 81,920 (82K) bytes in size (assuming each value
is five characters), but in binary form it will always be 32K (two bytes
per value).
The downside to binary files is that they really must be read via software intended for that purpose. A binary file can be opened in a binary file editor, but editing one is both tedious and error-prone. It’s much better to read it with software that inherently “knows” the structure of the file and can efficiently extract the data from it.
Binary data files are the preferred format for things like image data, large numeric arrays, and structured data that might contain either pure binary values or a mix of binary and ASCII. A compressed data file is an example of a binary file that is composed of structured binary data.
Flat Binary Data Files
Just like a flat ASCII file, a flat binary file consists of one or more records. Each record may be fixed-length, or, with a little extra work, they can be variable-length.
For example, what if you wanted to pass data to another program using a binary file, and that program expects records with an internal structure ...
Get Real World Instrumentation with Python now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.