Binary file

On this page 7

A binary file is a computer file whose contents are a sequence of bytes interpreted by a specific format, not by a character encoding, so the data is meant to be read by programs rather than by people. In software fundamentals, the term covers executables, images, archives, databases, compiled object code and many other files whose bytes can take any of the 256 possible values.

The distinction matters because tools treat the two classes of files differently. Text editors, version control systems, network transfer modes and diff utilities all assume that text files contain lines of characters, while a binary file must be handled byte for byte to avoid corruption.

How a binary file stores data

A binary file stores values in the native representation a program uses in memory or in a compact format defined by a specification. An integer such as 1,000,000 occupies exactly four bytes as a 32-bit value, whereas the same number written as text needs seven characters plus separators.

Most binary formats follow a common layout. A header at the start identifies the format and version, often with a fixed magic number; structured records or sections follow at known offsets; and an optional index or trailer allows random access without scanning the whole file. Because positions are measured in bytes, a single inserted or deleted byte can shift every later offset and invalidate the entire structure.

The following table lists widely recognized magic numbers that programs and the Unix file command use to identify formats regardless of the file name.

FormatFirst bytes (hex)Typical use
ELF7F 45 4C 46Linux and Unix executables and shared libraries
PE (MZ header)4D 5AWindows executables and DLL files
Mach-O (64-bit)CF FA ED FEmacOS and iOS executables
PNG89 50 4E 47Lossless raster images
ZIP50 4B 03 04Archives, Office documents, Java JAR files
PDF25 50 44 46Portable documents
SQLite53 51 4C 69 74 65Embedded relational databases
Common binary signatures at the start of a file

Binary file versus text file

At the storage level, every file is binary, because disks store only bytes. The practical difference lies in interpretation: a text file restricts itself to bytes that form valid characters in an encoding such as ASCII or UTF-8, organized into lines, while a binary file assigns meaning by position and format rules.

AspectBinary fileText file
InterpretationFormat specification and byte offsetsCharacter encoding and line breaks
Human readabilityRequires a dedicated program or hex viewerReadable in any text editor
Size efficiencyUsually compactLarger for numeric data
Parsing speedFast, often direct memory mappingSlower, requires tokenizing
Version control diffsOpaque, whole-file changesLine-by-line diffs and merges
Corruption toleranceOne wrong byte can break the fileDamage usually stays local
Practical differences between binary and text files

Tools use heuristics to tell the classes apart. Git, for example, treats a file as binary when it finds a NUL byte (0x00) in the first few kilobytes, and the file command combines magic numbers with character statistics. These heuristics can misjudge files in UTF-16, which contains many zero bytes yet is legitimate text.

Main categories of binary files

Binary files fall into several functional categories, and each category brings its own tooling and handling rules. The table below groups the most common ones and names a typical consumer for each.

CategoryExamplesTypical reader
Executable and object codeELF, PE, Mach-O, .o, .objOperating system loader, linker
Libraries.so, .dll, .dylib, .a, .libDynamic linker, build tools
BytecodeJava .class, Python .pyc, WebAssembly .wasmVirtual machine or runtime
MediaJPEG, PNG, MP3, MP4Decoders and players
Archives and containersZIP, tar.gz, ISO imagesArchivers, mount tools
Data and databasesSQLite, Parquet, Protocol BuffersDatabase engines, data libraries
Categories of binary files and the programs that read them

An executable file is the most familiar binary file in this field, because it carries machine code together with metadata that tells the operating system loader where to place each section in memory. The everyday phrase “the binary” for a compiled program comes from this category.

Byte order, alignment and portability

Binary formats are sensitive to hardware-level conventions. The most important is endianness: a little-endian system such as x86-64 stores the least significant byte first, while network protocols and many file formats use big-endian order. A format that does not declare its byte order cannot be read reliably on a different architecture.

  • Word size: a field written as a native long is 4 bytes on 64-bit Windows and 8 bytes on 64-bit Linux, so portable formats use fixed-width types such as 32-bit or 64-bit integers.
  • Alignment and padding: compilers insert padding between structure fields, so writing a C structure directly to disk produces compiler-dependent layouts.
  • Floating-point representation: nearly all modern hardware uses IEEE 754, but portable formats still state precision and byte order explicitly.
  • Versioning: a version field in the header lets newer software read older files and reject unknown layouts safely.

Well-designed formats such as Protocol Buffers, PNG and SQLite solve these issues by specifying every field precisely, which is why their files move between platforms without conversion.

Working with binary files safely

Handling a binary file correctly means preserving every byte unchanged unless a program intentionally edits the format. Several routine operations can silently damage binary data when they assume text.

MistakeConsequencePrevention
Opening in text mode on WindowsLine-ending translation alters bytesUse binary mode (rb, wb)
ASCII mode in FTP transfersCorrupted executables and archivesUse binary or image transfer mode
Editing in a text editorEncoding conversion breaks offsetsUse a hex editor or the native tool
Committing large binaries to GitRepository bloat, no meaningful diffsUse Git LFS or an artifact store
Trusting the file extensionWrong parser or security exposureCheck magic numbers and validate input
Common mistakes with binary files and how to avoid them

Binary parsers are also a classic attack surface. Malformed length fields and offsets have caused many buffer overflow vulnerabilities in image, font and document libraries, so robust software validates every size and offset before using it. Integrity checks such as SHA-256 checksums or digital signatures confirm that a downloaded binary has not been altered.

In plain words

A text file is like a letter: anyone who knows the alphabet can open it and read it. A binary file is more like a sheet of music written in a private code. The information is all there, but only the right program knows what each mark means.

Photos, songs, installed apps and compressed archives are all binary files. Opening them in a plain text editor shows gibberish, and saving them from that editor can damage them. The safe rule is simple: let the matching program handle the file.

A real-world example

A small development team keeps its web application in Git. A designer commits 300 MB of PNG and PSD files over a few months, and each revised image is stored again in full. The repository grows past 4 GB, and new developers wait more than twenty minutes for their first clone.

The team migrates image files to Git Large File Storage, which replaces each binary file in the history with a small text pointer and keeps the actual bytes on a separate server. It also adds a .gitattributes rule that marks the image extensions as binary, so Git never attempts line-ending conversion or text merges on them.

After the history is rewritten, the clone size drops to about 90 MB, and fresh clones finish in under a minute. The outcome shows the practical lesson: binary files need tooling that respects their format rather than tools designed for lines of text.