Introduction
Before Unicode, each system and each group of languages had its own code page: a
table that gives each byte value a character. The single-byte code pages have 256
characters. Their first half, 00 to 7F, is ASCII, and the
second half holds the letters and symbols of one region: Windows-1252 and ISO 8859-1
for Western Europe, Mac OS Roman on the classic Mac, code page 437 on the IBM PC,
Windows-1251, KOI8-R and code page 866 for Russian. Chinese, Japanese and Korean need
thousands of characters, so their code pages use two bytes for each one beyond ASCII:
GBK, Big5, Shift_JIS, EUC-JP and EUC-KR. Windows calls both kinds “ANSI code pages”,
and Notepad still saves in the computer’s own code page under the name “ANSI”.
Strictly, each code page is a format of its own: the byte E9 is é in
Windows-1252, È in Mac OS Roman, Θ in code page 437 and й in Windows-1251. But nothing
in a text file says which one was used. There is no byte order mark and no header, and
a single-byte code page accepts every byte, so the file reads without error in all of
them. This page therefore describes them together, as one format, and the explorer
guesses which code page a file uses.
The code page shown is a guess. The explorer says so each time, gives its reasons, and shows what the commonest bytes would be in each candidate, so that a reader can judge. No program can be sure, and with only a few accented letters in a file a guess is often wrong.
A file dropped on any page of this site opens in the explorer, which shows its structure (each line, and the bytes of any character beyond ASCII), the guessed code page with its reasons, and a preview of the text in that code page.
Structure
The file has up to three parts, in this order. Only the lines are always there.
0Lines characters of one byte, or in East Asian code pages one or two, each line followed by its line endingvaries…End-of-file mark Ctrl-Z (1A) bytes, from MS-DOS programssometimes…Zero padding NUL bytes after the textrare
Byte order mark
Code pages have no byte order mark, and nothing else in the file names the code page.
Formats built on text can say it, as HTML does with <meta charset>
and email with its Content-Type header, but a plain text file cannot.
A file that starts with a Unicode byte order mark is one of the Unicode formats, not
this one: EF BB BF UTF-8,
FF FE UTF-16LE,
FE FF UTF-16BE,
FF FE 00 00 UTF-32LE and
00 00 FE FF UTF-32BE.
One exception comes here. GB18030, China’s national encoding, writes U+FEFF as
84 31 95 33. The explorer has no GB18030 format yet, so such a file is
read as a code page; the explorer notes the mark and does not apply its Chinese
guess, since the text is not GBK.
Encoding
Single-byte code pages
Each byte is one character. To read it, look the byte up in the code page’s table
of 256 entries; to write a character, find it in the table and write its byte. A
character that is not in the table cannot be written at all, which is why each
region needed its own code page. Bytes 00 to 7F are ASCII
in every code page the explorer guesses between; 80 to FF
differ (see Code pages).
This 20-byte line was written in Windows-1252. Read in three code pages, the same bytes give three texts, and only one is right:
00000000 43 61 66 E9 20 93 71 75 6F 74 65 64 94 20 96 20 Caf. .quoted. .
00000010 35 B0 0D 0A 5...
| Read as | Text |
|---|---|
| Windows-1252 | Café “quoted” – 5° |
| Mac OS Roman | CafÈ ìquotedî ñ 5∞ |
| DOS code page 437 | CafΘ ôquotedö û 5░ |
| ISO 8859-1 | Café, then three invisible C1 control codes where the quotes and dash should be, and 5° |
The Russian word привет is EF F0 E8 E2 E5 F2 in Windows-1251,
D0 D2 C9 D7 C5 D4 in KOI8-R and AF E0 A8 A2 A5 E2 in DOS
code page 866.
Double-byte code pages
The East Asian code pages keep ASCII as single bytes and write every other
character as a lead byte above 7F followed by a trail byte. The lead
byte says that a second byte follows.
| Code page | Language | Windows code page | Bytes |
|---|---|---|---|
| GBK | Simplified Chinese | 936 | lead 81–FE, then one trail byte |
| Big5 | Traditional Chinese | 950 | lead 81–FE, then one trail byte |
| Shift_JIS | Japanese | 932 | lead 81–9F or E0–FC, then a trail byte 40–7E or 80–FC; A1–DF alone are half-width katakana |
| EUC-JP | Japanese, on Unix | – | pairs of bytes A1–FE; 8E then one byte for half-width katakana; 8F then two bytes (JIS X 0212), three in all |
| EUC-KR | Korean | 949 extends it | lead 81–FE, then one trail byte |
A trail byte is never 0A or 0D, so lines split in the
same places as in ASCII. A trail byte can be in the ASCII range, though. In
Shift_JIS it can be 5C, the backslash, which is why Japanese file paths
in Shift_JIS trip up programs that look for \ byte by byte.
日本語 (“Japanese”) in Shift_JIS is 93 FA 96 7B 8C EA: three
characters of two bytes each, the second with the trail byte 7B, which
alone would be {. 中文 (“Chinese”) in GBK is
D6 D0 CE C4.
Lines
A line is the characters up to a line
ending. The explorer counts one line for each line ending, plus one for any text
after the last, so a␊b␊ and a␊b are both two lines. Lines
are numbered from 1, as editors number them.
POSIX defines a line as ending in a newline, so a last line with no line ending is, strictly, incomplete. It is common and harmless, and the explorer gives it as a note, not a problem.
In a single-byte code page a character is a byte, so the counts of characters are
counts of bytes, not counting the line endings; the explorer shows the number of bytes
above 7F rather than of non-ASCII characters. When it guesses a
double-byte code page it reads the file again in that code page, so that the counts
are of characters. The longest line is measured in characters. Tabs (09)
and form feeds (0C, page breaks) are counted and not reported.
Line endings
In a code page only CR and LF end lines:
| Name | Bytes | Where it is used |
|---|---|---|
| LF (line feed) | 0A | Unix and Linux |
| CR LF | 0D 0A | Windows, MS-DOS, CP/M, Internet protocols |
| CR (carriage return) | 0D | classic Mac OS (up to 9), Apple II, Commodore |
The byte 85 is NEL (next line) in ISO 8859-1, but an ellipsis, …, in
Windows-1252, and a letter in most other code pages, so it is not taken as a line
ending. A file in Mac OS Roman usually ends its lines in CR alone, and the explorer’s
guess uses that (see The guess).
A file with more than one kind of ending is reported as having mixed line endings, with the first line that differs from the commonest kind.
End of the file
The text stops at the end of the file. Whether the last line has a line ending is covered under Lines. Two other things can follow the text:
- Ctrl-Z. CP/M stored file sizes in 128-byte records, so it marked
the end of the text with the byte
1A(SUB, Ctrl-Z), and MS-DOS programs kept writing it. It is common on old DOS text files, and the files examined for this site included some. The explorer shows trailing1Abytes as an End-of-file mark. A1Ainside the text is a control character. - Zero padding. NUL bytes after the text, left by programs that preallocate a file or write it in fixed-size blocks. The explorer shows them as Zero padding and reports them. A file that is mostly such zeros is not taken as text.
Problems
In a single-byte code page every byte is a character, so there are no invalid
sequences. Some bytes are unwelcome all the same: control codes, and the few bytes a
code page leaves unassigned, such as 81, 8D,
8F, 90 and 9D in Windows-1252. A reader shows
them as boxes, as nothing, or as U+FFFD. In a double-byte code page a lead byte with
no trail byte after it is an invalid sequence.
The worse problem is the wrong code page, which no reader can detect: every byte still reads as something. Text read in the wrong code page shows “mojibake”. é shown as È is Windows-1252 read as Mac OS Roman; é shown as é is UTF-8 read as Windows-1252.
| Problem | Meaning |
|---|---|
| NUL character | a 00 byte in the text |
| control character | a C0 control other than tab, LF, form feed and CR, or DEL (7F) |
| invalid byte sequence | in a double-byte code page, a lead byte with no trail byte |
| character cut short by the end of the file | in a double-byte code page, a lead byte as the very last byte |
| mixed line endings | more than one kind of line ending |
| zero padding | NUL bytes after the text |
The explorer adds notes when:
- the guess is Windows-1252 and the file holds bytes that Windows-1252 leaves unassigned, so the guess may be wrong;
- most multi-byte sequences in the first 4 MB are valid UTF-8: the file is then probably UTF-8 with a few damaged or mixed-in bytes;
- the file starts with
84 31 95 33, GB18030’s byte order mark; - the bytes look like EBCDIC, IBM mainframe text: the space,
40, is very common, most bytes are above80, and there are no line endings. The explorer does not decode EBCDIC.
Telling it apart
There is no positive test for a code page. This format is what is left: the explorer tries it last, after UTF-8, UTF-32 and UTF-16, and its parser accepts any file that none of those took and that still looks like text. NUL and control characters together must be under one in fifty of the first 64 KB. (Tab, LF, form feed, CR, escape and Ctrl-Z do not count.) That is tighter than the Unicode formats’ test, because a code page accepts any byte, so binary data would otherwise pass. A file that is mostly zero bytes is turned down.
Most files come here because UTF-8 turned them down: an accented letter written as
one byte, such as E9 for é, is a UTF-8 lead byte with no continuation.
A file of pure ASCII never comes here; it is valid UTF-8. Which code page it is, is
then guessed.
Code pages
Bytes 00 to 7F are ASCII in all of these. Some of the bytes
above, in the four Western code pages the explorer guesses between:
| Byte | Windows-1252 | ISO 8859-1 | Mac OS Roman | DOS 437 |
|---|---|---|---|---|
80 | € | C1 control | Ä | Ç |
82 | ‚ | C1 control | Ç | é |
84 | „ | C1 control | Ñ | ä |
85 | … | NEL (C1 control) | Ö | à |
8E | Ž | C1 control | é | Ä |
92 | ’ | C1 control | í | Æ |
93 | “ | C1 control | ì | ô |
96 | – | C1 control | ñ | û |
97 | — | C1 control | ó | ù |
A5 | ¥ | ¥ | • | Ñ |
B0 | ° | ° | ∞ | ░ |
C4 | Ä | Ä | ƒ | ─ |
C9 | É | É | … | ╔ |
CD | Í | Í | Õ | ═ |
D2 | Ò | Ò | “ | ╥ |
D5 | Õ | Õ | ’ | ╒ |
E9 | é | é | È | Θ |
FF | ÿ | ÿ | ˇ | no-break space |
And in the three Cyrillic code pages:
| Byte | Windows-1251 | KOI8-R | DOS 866 |
|---|---|---|---|
80 | Ђ | ─ | А |
B0 | ° | ╟ | ░ |
C4 | Д | д | ─ |
C9 | Й | и | ╔ |
E9 | й | И | щ |
FF | я | Ъ | no-break space |
Unicode’s mapping files and the WHATWG Encoding Standard give the full tables.
Windows-1252 and ISO 8859-1
ISO 8859-1 (Latin-1, 1987) is the Western European part of the ISO 8859 standard.
Its 80 to 9F are C1 control codes, which text does not
use. Windows-1252, the code page of Windows in Western Europe and the Americas,
agrees with it everywhere else, and puts printable characters in
80 to 9F: € ‚ ƒ „ … † ‡ ˆ ‰ Š ‹ Œ Ž ‘ ’ “ ” • – — ˜ ™ š › œ ž
Ÿ, leaving five bytes unassigned (81, 8D, 8F,
90, 9D). The euro sign at 80 was added later,
for the euro. The WHATWG Encoding Standard makes “iso-8859-1”, “latin1” and “ascii”
names for Windows-1252, so browsers read Latin-1 files as Windows-1252. A file with no
bytes in 80 to 9F reads the same in both, and the explorer
then names both.
ISO 8859-15 (Latin-9) changes eight bytes of 8859-1; A4 becomes €. The
explorer does not tell 8859-15 from 8859-1.
Mac OS Roman
The code page of the classic Mac OS from the first Macintosh (1984), in English and
Western European systems. It puts its accented letters in an order of its own,
starting at 80, and has typographic and mathematical symbols such as •,
∞ and ∫. Files from the classic
Mac OS end their lines in CR.
DOS code page 437
The character set of the IBM PC (1981). It has accented letters at
80 to A5, box-drawing and shading characters at
B0 to DF, and Greek letters and mathematical signs at
E0 to FE. DOS readmes drew frames and tables with the box
characters (╔═══╗), and the DOS tree command draws with them too
(├───). The PC also showed pictures for the control codes 01 to
1F (☺ ♥ ♪ …), but only on screen; in a file those bytes are still
control codes.
Cyrillic code pages
Windows-1251 is the Cyrillic code page of Windows, with the alphabet in order at
C0 to FF. KOI8-R (RFC
1489, 1993) was the usual code page of Russian Unix systems and email. It puts
the letters at C0 to FF in an order chosen so that, with
the top bit lost, each letter becomes a Latin look-alike: привет becomes PRIWET. DOS
code page 866 is the Russian DOS code page. It keeps 437’s box-drawing characters at
B0 to DF and puts the letters at 80 to
AF and E0 to EF.
Code pages the explorer does not guess
Many more exist, and a file in one of them is shown in whichever guessed code page fits best, which will be wrong. Among them: the rest of ISO 8859 (part 2 for Central Europe, 5 for Cyrillic, 7 for Greek and others), Windows-1250 and 1253 to 1258, DOS code page 850, KOI8-U, Mac OS Cyrillic, ISO-2022-JP (a 7-bit Japanese encoding with escape sequences), and GB18030. EBCDIC, the IBM mainframe family, is not ASCII-based at all.
The guess
No rule can tell these code pages apart. Every byte is valid in each of them, so the only evidence is which reading looks more like real text. Tools such as chardet, uchardet and ICU’s detector all work this way, from statistics, and all can be wrong. The explorer’s guess is the same kind of thing. Step by step:
- Count the bytes. Every byte value is counted over the whole file, and so is how often each byte follows itself.
- Density. The bytes above
7Fare compared with the ASCII letters. In Western European text they are a few accented letters among many ASCII ones. In Russian text nearly every letter is one, so when they make up more than 0.35 of the two together, the text is taken to be mostly non-Latin, and the guess is between Windows-1251, KOI8-R and DOS 866, with DOS 437 kept in for box drawing. Otherwise it is between Windows-1252, Mac OS Roman and DOS 437. - Score each candidate. Each byte above
7Fis read in the candidate code page and weighted by how common that character is in ordinary text: Western European letters (é, à, ü, ñ, ø …) and English typography (’ “ ” – … •), or Russian letter frequencies for the Cyrillic code pages. A C1 control code or an unassigned byte counts −3, a symbol −0.5. A letter that follows itself is not counted, since letters seldom double. - Box drawing. Box-drawing characters count only when they repeat (═════), as in frames and shading; a lone one counts slightly against.
- Line endings. Lines that end in CR, with no CR LF and hardly any LF, point to the classic Mac OS, and Mac OS Roman’s score is raised.
- The winner. The highest score wins. If it is Windows-1252 and the
file has no bytes from
80to9F, the result is named “Windows-1252 or ISO 8859-1”, since the two agree on every other byte. With fewer than 20 bytes above7Fthe explorer says the guess is weak. - Box art. In a dense file, when bytes
B0toDF, box drawing in both DOS code pages, are most of the bytes above7Fand at least three in ten of them repeat the byte before, the file is DOS 437, or 866 if 866’s Cyrillic letters are common as well. A listing from the DOStreecommand is the typical case. - East Asian code pages. Also in a dense file, the start (up to 64 KB, cut at a line feed) is decoded strictly in GBK, Big5, Shift_JIS, EUC-JP and EUC-KR. A code page counts only if the start decodes without an error and at least half of what is not ASCII comes out as kana, Chinese characters or Hangul. Among those, the one whose text holds the most hiragana (for Japanese), the most of the commonest Chinese characters (的 一 是 不 了 …), or the most of the commonest Korean syllables (이 다 는 의 에 …) wins. The file is then read again in it.
The explorer shows the result as the encoding, marked “a guess”, with its reasons,
and a table of the commonest bytes above 7F with what each would be in
every candidate. When the guess looks wrong, that table usually shows the right code
page.
In spot checks on the files examined for this site the guess read correctly DOS
readmes with box drawing and tree listings (437), Mac OS 7 era release
notes (Mac OS Roman, with • and ∫ as bullets), Russian and Bulgarian documentation
(1251), Russian Unix documentation (KOI8-R), Chinese readmes (GBK) and Japanese
readmes (Shift_JIS). Short files with only one or two accented letters remain the
hardest case.
Variants
- UTF-8 text – one to four bytes a character, ASCII unchanged
- UTF-16LE text – two or four bytes a character, low byte first
- UTF-16BE text – two or four bytes a character, high byte first
- UTF-32LE text – four bytes a character, low byte first
- UTF-32BE text – four bytes a character, high byte first
- Legacy code page text – a pre-Unicode code page that the file does not name
Links
Software
- Notepad – the text editor built into Windows, which opens and saves the computer’s own code page as “ANSI” (Windows)
- Notepad++ – free text and code editor that reads a file in any of many character sets and converts it to UTF-8 (Windows)
- Visual Studio Code – free code editor that can reopen a file in a chosen encoding (Windows, macOS, Linux)
- BBEdit – long-standing Mac text editor that opens Mac OS Roman and other old encodings (macOS)
- Vim – keyboard-driven text editor (Windows, macOS, Linux)
- GNU Emacs – extensible text editor (Windows, macOS, Linux)
- iconv – command that converts a file from a code page to UTF-8 (macOS, Linux)
- file – the Unix
filecommand, which names a text file’s encoding and line endings (macOS, Linux)
Developers
- ICU – conversion between encodings and charset detection (C, C++, Java)
- GNU libiconv – the iconv conversion library, with many code pages (C)
- chardet – guesses a file’s encoding (Python)
- uchardet – encoding detector from Mozilla’s code (C)
- encoding_rs – the WHATWG encodings, as used by Firefox (Rust)
- ftfy – repairs mojibake (Python)
Reference
- WHATWG Encoding Standard – the code pages browsers support, with their tables
- Unicode’s code page mapping files – CP437, CP1252, Mac OS Roman and others, byte by byte
- RFC 1489 – KOI8-R