The guide
A file cannot name its own encoding, but its bytes can rule encodings out
A text file is only numbers. Strict checks can prove that an encoding cannot read them and show where, but only the author knows which one was meant.
- 27 of 32 bytes 80 to 9F that windows-1252 gives a character to
- 256 of 256 byte values that ISO 8859-1 reads, so it is never ruled out
- C3 A9 UTF-8 for one accented letter; two odd characters if read singly
- FF FE 00 00 a UTF-32 mark, or a UTF-16 mark followed by an empty character
- U+10FFFF the highest code point that UTF-8 and UTF-16 can reach
Read the full guide Fold the guide
Open a text file in the wrong program and the letters turn to nonsense. Nothing in the file changed. The bytes are the same as before, and only the table used to turn them into characters is different.
A file is numbers, and the label is missing
A text file stores numbers. An encoding is the agreed table that says which character each number, or each short group of numbers, stands for. Most files carry no note saying which table was used, so a program has to be told, or has to guess.
ASCII settled the first 128 numbers long ago, and nearly every encoding in common use keeps those 128 the same. That is the good news and the trap. A file of plain English letters, digits and punctuation reads identically under all of them, so it carries no evidence about which one was meant. Only bytes at 80 and above can tell the tables apart.
Two single-byte tables show how different the evidence can be. ISO 8859-1 gives a character to all 256 byte values once the control codes are added, as the registered Internet name for it does. It can therefore read any file at all, and for the same reason it can never be ruled out. The table windows-1252 keeps the same characters in most places but fills the range 80 to 9F with printable symbols such as curly quotation marks and the euro sign. The Unicode data file for it defines 27 of those 32 positions and leaves five undefined: 81, 8D, 8F, 90 and 9D. A strict reader therefore refuses a file that contains one of those five.
That asymmetry is the first useful fact. One of these two tables can be ruled out by a single byte, and the other never can. Browsers blur it: the HTML standard has them treat the label for ISO 8859-1 as windows-1252, so asking a browser for strict Latin-1 reading is not possible, and a strict single-byte check has to be written from the tables themselves.
Why valid UTF-8 is strong evidence
UTF-8 writes each character in one to four bytes. ASCII characters stay as themselves. Every other character starts with a lead byte, from C2 to F4, followed by one to three continuation bytes in the range 80 to BF. The lead byte says how many continuation bytes to expect.
The rules are tight, and the tightness is the point. RFC 3629 gives the grammar. C0 and C1 never start a sequence, because they could only introduce an overlong form, a character written in more bytes than it needs. After E0 the next byte must be A0 to BF. After ED it must be 80 to 9F, which keeps out the surrogate range D800 to DFFF. After F0 it must be 90 to BF, and after F4 it must be 80 to 8F, so nothing goes past U+10FFFF. The bytes F5 to FF never occur at all.
When a decoder meets a break, it has to decide what to do. Wikipedia describes the recommended practice: treat the broken piece as one error that ends at the first byte that cannot continue the sequence, show the replacement character U+FFFD in its place, and go on from the next byte.
Most text in other encodings does not survive this test once it contains accented letters. Wikipedia works out that in a random run of bytes, a byte with the top bit set has only about a one in fifteen chance of starting a valid UTF-8 character, and the chances compound over a whole file. A file with no break at all, and with bytes above 7F in it, is very probably UTF-8. Very probably is still not certain, and the page on the byte order mark names the one practical exception: text that is entirely ASCII.
So the verdict has two strengths. A break is certain, because the rule is a rule. A pass is a strong hint when the file has bytes above 7F and no hint at all when it does not.
The mark at the start, and the pattern of zeros
The byte order mark is the character U+FEFF written at the very start of a file. In UTF-8 it is the three bytes EF BB BF. In UTF-16 it is FE FF for big endian and FF FE for little endian. In UTF-32 it is 00 00 FE FF or FF FE 00 00. Wikipedia lists them in one table.
Its use is optional. The Unicode Standard permits it in UTF-8 but neither requires nor recommends it, and it can trip up software that does not expect non-ASCII bytes at the start of a file. A file without a mark can be perfectly good UTF-8, and a file with one can still fail the UTF-8 rules further in. RFC 2781 goes further for UTF-16: text labelled as UTF-16LE or UTF-16BE must not carry a mark, because the label already fixes the byte order.
The mark can also be ambiguous. The four bytes FF FE 00 00 are the little-endian UTF-32 mark. They are also the little-endian UTF-16 mark followed by the character U+0000, which is a legal, if unusual, way to begin a file. Wikipedia says a programmer must decide which is more likely, and that is a judgement, not a measurement. A fair checker shows both readings.
Without a mark, UTF-16 betrays itself through zeros. Basic Latin characters have a zero high byte, so mostly-ASCII text in UTF-16 has a 00 at every second position: odd positions for little endian, even positions for big endian. UTF-32 does the same on a larger scale, because its high byte is always zero and the next one is zero for any character below U+10000. For ASCII text, three bytes in every four are 00.
The reverse is the trap. A strict UTF-16 check only asks for whole two-byte units and no unpaired surrogate, and almost every 16-bit value is a code point. Any file of even length with no stray D800 to DFFF value passes. A pass is weak unless the zeros fall where they should.
How mojibake arises
Mojibake is the garbage that appears when bytes are read with the wrong table. The commonest case is UTF-8 read as a single-byte encoding. The accented letter e with an acute accent is C3 A9 in UTF-8. A single-byte reader sees two characters, U+00C3 and U+00A9, and shows them one after the other: a capital A with a tilde, then a copyright sign. That pair, or something like it, is the usual signature.
Here is a worked example you can reproduce by pasting the bytes into the page. Take 63 61 66 C3 A9. Read as UTF-8 it is valid: three ASCII letters and one two-byte character, the word with its accent. Read as windows-1252 it is also valid, because no byte is undefined, and it shows c, a, f, U+00C3 and U+00A9.
Now take 63 61 66 E9, the same word saved one byte per letter. Read as windows-1252 it is the word with its accent. Read as UTF-8 it fails at offset 3, because E9 promises two continuation bytes and the file ends. UTF-8 is ruled out for the second file and not for the first, which is exactly the difference a human cannot see in the bytes.
The reverse mix-up shows as the replacement character. Single-byte text read as UTF-8 loses each accented letter to U+FFFD, and the original byte is gone from the display. A text can also be converted twice. If a file is valid UTF-8, and every character in it exists in windows-1252, writing those characters back as windows-1252 bytes may give valid UTF-8 again. When it does, the file probably holds text that was read with the wrong table once and then saved.
Japanese, Chinese and Korean text makes it harder. Wikipedia notes that a browser may not tell a page in EUC-JP from one in Shift-JIS when no label is given, and that is a statement about the encodings, not about the browser. A check with Python’s codecs on a sample of eight Japanese characters found that the 16 Shift_JIS bytes are also accepted by the gbk, cp1252 and latin-1 decoders. Accepting the bytes says nothing about whether the result is the intended text.
Ask which encodings are possible
The useful question is not which encoding a file is in. For most files that has no certain answer. The useful question is which encodings can read every byte without error, and where each of the others first breaks. That one always has an answer, and the answer can be checked.
A browser can do most of it. A decoder made with the option for fatal errors throws on invalid data instead of substituting a replacement character, according to MDN, and its decode call can take a streaming flag so that a sequence split across two slices is not mistaken for an error. A file can be read in slices with the Blob slice method, a megabyte at a time, so a large file is never held whole. Heavy loops belong in a worker, so that the page stays responsive and a cancel can be heard between slices.
Some limits stay. A decoder that refuses tells you a byte, not the reason, so the checker has to work out where the break lies. The legacy decoders in a browser follow their own tables and can differ from other programs. A file made only of ASCII passes everything. And no byte test can tell you the language, the author’s intent, or whether a pass is a coincidence. The person who wrote the file is the only authority on which encoding was meant, and the best a checker can do is narrow the field honestly.
Sources
Every source consulted for this guide. Links checked November 9, 2026.
- UTF-8 - Wikipedia Wikipedia (CC BY-SA)
- Byte order mark - Wikipedia Wikipedia (CC BY-SA)
- Windows-1252 - Wikipedia Wikipedia (CC BY-SA)
- ISO/IEC 8859-1 - Wikipedia Wikipedia (CC BY-SA)
- UTF-16 - Wikipedia Wikipedia (CC BY-SA)
- Mojibake - Wikipedia Wikipedia (CC BY-SA)
- Specials (Unicode block) - Wikipedia Wikipedia (CC BY-SA)
- RFC 3629: UTF-8, a transformation format of ISO 10646 IETF
- RFC 2781: UTF-16, an encoding of ISO 10646 IETF
- cp1252 to Unicode table Unicode, Inc. (Unicode data file)
- ISO/IEC 8859-1:1998 to Unicode Unicode, Inc. (Unicode data file)
- TextDecoder - Web APIs MDN Web Docs (CC BY-SA)
- TextDecoder: decode() method - Web APIs MDN Web Docs (CC BY-SA)
- Blob: slice() method - Web APIs MDN Web Docs (CC BY-SA)
- Web Workers API MDN Web Docs (CC BY-SA)