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.
| Format | First bytes (hex) | Typical use |
|---|---|---|
| ELF | 7F 45 4C 46 | Linux and Unix executables and shared libraries |
| PE (MZ header) | 4D 5A | Windows executables and DLL files |
| Mach-O (64-bit) | CF FA ED FE | macOS and iOS executables |
| PNG | 89 50 4E 47 | Lossless raster images |
| ZIP | 50 4B 03 04 | Archives, Office documents, Java JAR files |
| 25 50 44 46 | Portable documents | |
| SQLite | 53 51 4C 69 74 65 | Embedded relational databases |
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.
| Aspect | Binary file | Text file |
|---|---|---|
| Interpretation | Format specification and byte offsets | Character encoding and line breaks |
| Human readability | Requires a dedicated program or hex viewer | Readable in any text editor |
| Size efficiency | Usually compact | Larger for numeric data |
| Parsing speed | Fast, often direct memory mapping | Slower, requires tokenizing |
| Version control diffs | Opaque, whole-file changes | Line-by-line diffs and merges |
| Corruption tolerance | One wrong byte can break the file | Damage usually stays local |
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.
| Category | Examples | Typical reader |
|---|---|---|
| Executable and object code | ELF, PE, Mach-O, .o, .obj | Operating system loader, linker |
| Libraries | .so, .dll, .dylib, .a, .lib | Dynamic linker, build tools |
| Bytecode | Java .class, Python .pyc, WebAssembly .wasm | Virtual machine or runtime |
| Media | JPEG, PNG, MP3, MP4 | Decoders and players |
| Archives and containers | ZIP, tar.gz, ISO images | Archivers, mount tools |
| Data and databases | SQLite, Parquet, Protocol Buffers | Database engines, data libraries |
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
longis 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.
| Mistake | Consequence | Prevention |
|---|---|---|
| Opening in text mode on Windows | Line-ending translation alters bytes | Use binary mode (rb, wb) |
| ASCII mode in FTP transfers | Corrupted executables and archives | Use binary or image transfer mode |
| Editing in a text editor | Encoding conversion breaks offsets | Use a hex editor or the native tool |
| Committing large binaries to Git | Repository bloat, no meaningful diffs | Use Git LFS or an artifact store |
| Trusting the file extension | Wrong parser or security exposure | Check magic numbers and validate input |
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.