Binary vs Text Files: What's the Difference?
By SimpleTools24Published 4 min read
Every file on a computer is made of bytes. A text file is one whose bytes are meant to be read as characters using a character encoding such as UTF-8, with line-break characters separating lines. A binary file is everything else: its bytes encode numbers, pixels, sound samples, machine instructions or compressed data in a structure defined by a specific format. The difference isn't in the storage; it's in how the bytes are intended to be interpreted.
In this guide
The Same Bytes, Two Interpretations
Take the four bytes 48 69 21 0A. Interpreted as UTF-8 text, they read Hi! followed by a new line. Interpreted as a little-endian 32-bit integer, they're the number 169,961,800. Neither reading is "wrong" in a technical sense; what the file is supposed to be determines which one makes sense.
That's why opening a binary file in a text editor shows nonsense, and why a hex viewer can show any file: it doesn't interpret the bytes at all, it just displays them. How to read a hex dump explains that view.
What Makes a File "Text"
A Character Encoding
Text files rely on an encoding that maps bytes to characters:
- ASCII uses values 0–127 for English letters, digits, punctuation and control codes.
- UTF-8 is ASCII-compatible and uses two to four bytes for other characters, such as
é(C3 A9) or€(E2 82 AC). It's the dominant encoding on the web and in modern software. - UTF-16 uses two or four bytes per character and appears in some Windows files.
- Legacy code pages like Windows-1252 or ISO-8859-1 map single bytes to Western European characters.
If a program guesses the wrong encoding, you get mojibake: café instead of café.
Line Endings
Text is organised in lines, and the end of a line is marked by control characters:
| System | Bytes | Name |
|---|---|---|
| Linux, macOS, most modern tools | 0A | LF (line feed) |
| Windows | 0D 0A | CRLF (carriage return + line feed) |
| Classic Mac OS (before OS X) | 0D | CR |
Mixed line endings in one file are a common source of odd diffs and scripts that misbehave.
Optionally, a Byte Order Mark
Some text files begin with a byte order mark (BOM): EF BB BF for UTF-8, or FF FE / FE FF for UTF-16. It signals the encoding, but some tools don't expect it and show stray characters at the start of the file.
What Makes a File "Binary"
Binary formats store data in the form a program uses directly:
- Numbers as fixed-size integers or floating-point values, not digit characters. The number 1000 takes four bytes as an integer (
E8 03 00 00in little-endian), versus four characters31 30 30 30as text. - Structures with headers, offsets and lengths that point to other parts of the file.
- Compressed or encrypted data, which looks random by design.
- Signatures at the start that identify the format; see file signatures and magic numbers.
Images, audio, video, archives, executables, databases and most office documents are binary.
How Tools Decide Whether a File Is Binary
Programs can't know your intention, so they guess. Common heuristics:
- Null bytes. Text in ASCII or UTF-8 essentially never contains
00. Git,grepanddifftypically treat a file with null bytes as binary. - Invalid encoding. Bytes that don't form valid UTF-8 sequences suggest binary data (or a different encoding).
- Control characters. Lots of bytes below
20other than tab, LF and CR point to binary content. - Known signatures. A file starting with
%PDForPKis recognised as a format, not as text.
These heuristics occasionally misfire. UTF-16 text contains many 00 bytes, so some tools label it binary. That's why grep sometimes reports "Binary file matches" on what you thought was a log.
Text-Based Formats That Behave Like Binary Ones
Some formats are text but structured: JSON, XML, CSV, HTML, SVG. You can open them in an editor, but their meaning comes from syntax rules. Conversely, some binary formats contain lots of readable text: a PDF often has visible keywords between compressed streams, and executables contain readable messages.
For huge text-based files, a text viewer is the right tool; why large text files crash text editors explains why size matters so much there.
Why the Difference Matters in Practice
- Transfers. Old FTP "ASCII mode" converted line endings and corrupted binary files sent the wrong way.
- Version control. Git can show line-by-line diffs for text but not for binary files.
- Editing. Saving a binary file from a text editor can silently change bytes, such as line endings or invalid sequences, and break it.
- Searching. Text search tools may skip binary files or print warnings instead of matches.
Checking a File Yourself
The quickest test is to look at the bytes. Open the file in a binary file viewer and check the ASCII column: readable sentences separated by 0A or 0D 0A mean text; dots, 00 runs and a format signature mean binary. The viewer works in your browser without uploading the file, and its Strings tab pulls readable text out of binary files. If you're starting from scratch, how to view a binary file covers the options.
Frequently Asked Questions
Is a text file also a binary file?
Technically all files are binary, since they're stored as bytes. "Binary file" is used for files whose bytes aren't meant to be read as characters.
Is JSON binary or text?
JSON is text, encoded in UTF-8 by specification. Binary alternatives such as BSON or MessagePack exist for efficiency.
Why does Git say my file is binary?
Usually because it contains null bytes, often from UTF-16 encoding. Converting the file to UTF-8 typically fixes it.
Can I convert a binary file to text?
You can represent it as text, for example as a hex dump or Base64, but that's an encoding of the bytes, not a readable document.
Summary
Text files are bytes meant to be decoded as characters and lines; binary files are bytes structured for a program. Encodings, line endings and null bytes are the clues, and a hex view shows you which kind you have.
Related tools
Sources
- The Unicode Standard, UTF-8 encoding form – Unicode Consortium (opens in a new tab)
Related articles
- How to Read a Hex DumpA line-by-line walkthrough of offsets, hex bytes, the ASCII column and little-endian numbers.
- How to View a Binary FileSee inside .bin, .dat and unidentified files with hex viewers, built-in commands and a browser-based tool.
- Why Big Files Crash EditorsThe memory, line-indexing and rendering work that makes editors hang on big files, and how viewers avoid it.