Caxton
Features Benchmarks Pricing Docs How it works Changelog Support About Download Free
Guides
All guides Large File Text Editors EmEditor for Mac UltraEdit Alternatives Notepad++ Alternatives Notepad for Mac Free Text Editors for Mac File Too Large for VS Code CSV Editor for Mac Open a CSV File CSV Viewer for Mac Open a TSV or Pipe-Delimited File Edit CSV Without Auto-Formatting Repair a Malformed CSV Remove Duplicate Rows Validate a CSV Transform a CSV Column Fixed-Width to CSV Change a CSV Delimiter Split a Large CSV Combine CSV Files Sort a Large CSV CSV Too Big for Excel Open a Huge CSV Log Viewer for Mac View Log Files Follow a Live Log File CMTrace for Mac Open a 10 GB Log Filter Large Log Files Jump to a Time in a Log Find & Replace in Huge Files Find & Replace Across Files Regex Cheat Sheet Compare Two Text Files Open a SQL Dump Fix Garbled Text & Encoding Convert a File to UTF-8 Remove a BOM Convert Line Endings Remove Invisible Characters Smart Quotes to Straight Quotes Sort & Dedupe Lines Open a GBK or Big5 File Open a UTF-16 or Shift-JIS File Open a Large JSON File Numbers File Too Large

How to fix garbled text and the wrong encoding on a Mac

Text that shows é where é should be, or a row of Ð and Ñ where Cyrillic should be, usually means the file was read with the wrong encoding. If the bytes are intact, reopening with the right encoding brings the text back. After that you can convert the file to UTF-8 so it stops happening. The Terminal does both for free, and the commands are below.

For UTF-16 and Japanese files, how to open a UTF-16 or Shift-JIS file on a Mac lists what Caxton detects and what you choose by hand.

Download Caxton for Free · 7-day free trial of Caxton Pro, no credit card required · 8 MB · macOS 13.0+

The short answer

The free fix is in Terminal: file guesses the encoding and iconv converts the file to UTF-8. Caxton, a native Mac editor for text and data files, detects the encoding when a file opens, offers Reopen with Encoding when the guess was wrong, which brings the text back without touching the bytes, and can save the file as UTF-8.

What is mojibake?

A text file is bytes, and an encoding is the table that turns bytes into characters. The file does not say which table it was written with. When a program guesses the wrong one, every byte still maps to something, so nothing fails: the text just comes out as the wrong characters. The Japanese word for that result, mojibake, is the one that stuck.

The patterns repeat, which makes them readable once you have seen them:

What it should sayWhat you seeWhat happened
ééUTF-8 text read as Windows-1252. One accented letter is two bytes in UTF-8, and each byte became its own character.
ПриветПриветUTF-8 Russian read as Windows-1252. Every Russian letter in UTF-8 starts with one of two bytes, which Windows-1252 shows as Ð and Ñ.
НаименованиеÍàèìåíîâàíèåWindows-1251 text read as Windows-1252. One byte per letter in both, so the length is right and every letter is wrong.

Why does detection get it wrong?

Because for most legacy encodings there is nothing to detect. Three cases account for nearly all of it.

  • Single-byte encodings all look valid. Windows-1252 (Western European) and Windows-1251 (Cyrillic) both give a meaning to nearly every byte. The byte E9 is é in one and й in the other, and both are perfectly legal. A detector cannot test validity; it can only decode the text each way and score which result reads more like a language. On a file that is mostly numbers and IDs, there is not much to score.
  • UTF-16 without a byte order mark. The Unicode FAQ describes the BOM as a signature "defining the byte order and encoding form," and says unmarked UTF-16 "uses big-endian byte serialization by default." Windows tools commonly write it little-endian, with or without the mark, so a reader has to infer the order from where the zero bytes fall.
  • ASCII-heavy heads. A log or a CSV can be plain ASCII for its first million lines. A detector that samples the start of the file sees valid UTF-8, because ASCII is valid UTF-8, and the one customer name with an accent is far below where it looked.

What are the free routes on a Mac?

Two commands that ship with macOS. file -I identifies, and iconv converts.

file -I export.csv

The manual page describes -I this way: "Causes the file command to output mime type strings rather than the more traditional human readable ones." The output includes a charset. Treat that charset as a hint. For this guide we made a Windows-1251 CSV and asked: file answered charset=iso-8859-1. It can tell that the bytes are not UTF-8. It cannot tell which single-byte encoding they are, for the reason above.

iconv -f WINDOWS-1251 -t UTF-8 export.csv > export-utf8.csv
iconv -l

iconv "converts the codeset of file" from the one you name with -f to the one you name with -t, and iconv -l lists the names it accepts. The working method is to try a candidate, look at the first lines of the result, and try the next one if it still reads wrong. Where the file came from narrows it quickly: a Windows system in Western Europe or the Americas usually means Windows-1252, a Russian one Windows-1251, a Japanese one Shift-JIS.

Opening an "ANSI encoded" file on a Mac

A file described as ANSI usually came from Windows, where the word means the system's default code page rather than any single standard. Microsoft's table of code page identifiers lists 1252 as "ANSI Latin 1; Western European (Windows)," the default for most of Western Europe and the Americas, and 1250 as "ANSI Central European," with a warning worth repeating: "ANSI code pages can be different on different computers, or can be changed for a single computer, leading to data corruption." So an ANSI file from a Western European or American Windows machine is almost always Windows-1252, and one from Poland or the Czech Republic is Windows-1250.

Read as UTF-8, which is what a Mac assumes, a Windows-1252 file keeps every plain letter and loses the accented ones: each é or ü is a single byte that is not valid UTF-8 on its own, so it shows as a replacement character, the question mark in a diamond, or is dropped. The reverse mistake looks different, which is how you tell them apart: a UTF-8 file read as Windows-1252 shows two characters for each accented letter, such as é for é.

The free route is iconv, naming the code page as the source:

iconv -f WINDOWS-1252 -t UTF-8 report.txt > report-utf8.txt

In Caxton, File ▸ Reopen with Encoding lists Windows-1252 and Windows-1250 among its encodings, and when a file opens as UTF-8 with bytes that look like Windows-1252 the banner suggests that encoding and the menu preselects it. Reopening changes nothing in the file; Save As writes the reopened file as UTF-8. Reading it is Caxton Free at any size. Saving the converted copy is Caxton Free for a file up to 10 MB, and needs Caxton Pro for a larger one.

Where do the free routes stop?

At seeing. Each guess is a new copy of the file, and you judge it from a few lines in a terminal. iconv converts what you tell it to; it has no opinion about whether you were right. For a file you need to read, check, and then edit, the loop of convert, inspect, and delete gets slow.

How does Caxton handle it?

What detection does when a file opens

A byte order mark is definitive. Without one, the file is checked as UTF-8, and the check samples windows through the file plus its tail rather than only the head, so the accented name on line 400,000 gets a vote. A few bad bytes in a file that is otherwise UTF-8 are treated as damage, not as a reason to reread the whole file. Beyond that, a window that is not valid UTF-8 brings the legacy encodings in, and then the candidates are scored by how the decoded text reads. Western European, Cyrillic, Central European, Japanese, Chinese, and Korean encodings are detected this way.

A legacy detection never claims to be certain, so the hint stays visible. When a file opens as UTF-8 and some bytes look like something else, the banner names the candidate ("Some bytes look like Windows-1252. Reopen with that encoding?") and the menu preselects it.

Why did Caxton read the file in this encoding?

View ▸ Show Inspector opens the Inspector, which shows the reasoning. Its Encoding section names the encoding and the confidence of the detection, and a row named Why this encoding gives the reason in a sentence or two, such as "Caxton read this file as Windows-1251 because it is not valid UTF-8 and its bytes fit Cyrillic text best. KOI8-R was the next closest." The reason can be a byte order mark, bytes that are all valid UTF-8 with a count of the multi-byte sequences, plain ASCII, the zero bytes of UTF-16 text, the legacy encoding the bytes fit best with the next closest one, your own choice in Reopen with Encoding, or a file pattern rule.

That reason points to the fix. When a second encoding is named as the next closest, it is the first one to try in Reopen with Encoding. When the file is valid UTF-8 and the text still shows é, no other encoding will read it better, because the garbled characters are what the file holds. Repair Mojibake, further down, is for that file.

In the same section the Invalid bytes row counts what could not be read in that encoding, with First and Next to jump to each place. The Selection section names the character at the caret with its code point and its bytes in the file's encoding, so you can see what a garbled letter really is. All of this is reading, so it is Caxton Free at any file size.

Reopen with Encoding

File ▸ Reopen with Encoding reinterprets the file in any of the 18 encodings Caxton reads: UTF-8 with or without a BOM, UTF-16 in both byte orders, Shift-JIS, EUC-JP, GB18030, Big5, EUC-KR, Windows-1252, ISO-8859-1, Windows-1251, KOI8-R, ISO-8859-5, Windows-1250, ISO-8859-2, Windows-1254, and Windows-1257. The last two, Turkish and Baltic, are in the menu but are not detected; choose them when you know. A forced encoding wins over the byte order mark, which matters for the file that carries a BOM it does not deserve. Transcoding runs in the background with progress and a cancel, and one undecodable byte does not fail the open: only the bytes that cannot be decoded become replacement characters.

The same CSV in Caxton read as Windows-1252, garbled, and then reopened as Windows-1251, showing correct Cyrillic text

One file, read two ways. Above, a Windows-1251 CSV read as Windows-1252. Below, the same bytes after Reopen with Encoding. The file is synthetic: a made-up warehouse export, every value invented.

European CSVs

The file in the picture is also the other common European shape: semicolons between fields and decimal commas in the prices. Caxton's delimiter detection does not count a comma that mostly sits between digits, so that file opens as the seven columns it has. If a file still reads wrong, CSV ▸ Delimiter sets how it is read without changing it, and structural problems past that are in the guide to repairing a malformed CSV.

Saving, and converting to UTF-8

Reopening does not touch the file. You are reading the same bytes through a different table, and a status bar message names the source encoding and adds that you are editing a UTF-8 working copy. ⌘S converts back and writes the file in its original encoding, so a Windows-1251 file stays one for the system that expects it. If you have typed a character that encoding cannot hold, Caxton stops and offers to save as UTF-8 instead of writing something lossy. Save As is the conversion: it writes UTF-8, and a line in the save panel tells you that before you click. File ▸ Save with Encoding… converts the file itself, to UTF-8 or to any other encoding in the Reopen menu. It first makes sure every character can be written in the encoding you chose, and when any cannot it counts them, offers to take you to the first, and saves nothing. If the file already reads correctly and the only job is to change its encoding, how to convert a file to UTF-8 on a Mac is the page for that.

When the garbled text was saved into the file

Sometimes a program read UTF-8 text in the wrong encoding and then saved what it saw, so the file itself now holds é and no encoding reads it back. Text ▸ Clean Up ▸ Repair Mojibake is for that file. It works on the selection, or on the whole document when nothing is selected, and it opens a preview before it changes anything.

It repairs text that was written as UTF-8, read as Windows-1252, ISO 8859-1, Windows-1251 or Mac OS Roman, and saved that way. é becomes é again, ’ becomes a curly apostrophe, and the row of Ð and Ñ in the table at the top of this page becomes Russian. Text that went through the mistake twice, such as é, is repaired twice. The preview counts the runs it will repair by the encoding they were misread in, and it shows a few of them as they are and as they will be.

It leaves alone anything that could be ordinary text as it stands. ALLÔ is ALLÔ misread, and it is also how a word in capitals reads before a closing quotation mark, so on its own it stays. The rest of its line decides: where everything else from outside plain ASCII was misread, it is repaired with the rest, and where the line also holds accented text that is intact, it stays. When a place that stays shares a line with mojibake that is repaired, the preview counts it. A sign alone that was read as Windows-1251, such as a degree sign that shows as В°, is left when nothing else on its line was misread, because the same two characters are also how a Russian word, unit or name is written. A run that lost a byte on the way, such as †with nothing after it, cannot be put back, so the preview counts it as damaged and it stays as it is.

Repair Mojibake is not for a file that only opened in the wrong encoding. That file's bytes are right, and Reopen with Encoding reads them without changing one. It also cannot put right text that never was UTF-8: the third row of the table at the top of this page, a Windows-1251 file read as Windows-1252, is a different mistake.

Words of mixed case come back whole, such as the Russian units МВт and МГц or the name МиГ. A word the repair cannot bring back in full is left as it is and is not counted, so the count in the preview covers whole words only. One shape can still be left garbled: a degree sign after a number, as in 90°. Read the samples in the preview before you apply.

The repair applies as one undo step, changes no line ending, and handles up to 256 MB in one step. If the text it would change holds bytes that are not valid UTF-8, it does not run, and a message names the first such line and points to Reopen with Encoding. The preview opens on Caxton Free in a file of any size, and applying the repair is Caxton Pro at any size.

The file that arrives every week

A file pattern rule can set the encoding for a glob, so the vendor export that is always Shift-JIS opens correctly without the menu.

Open the garbled file. Reopen it with the right encoding. Read it.

Download Caxton for Free

7-day free trial of Caxton Pro, no credit card required · 8 MB · macOS 13.0+ · notarized

Which approach fits which job?

ApproachGood forBreaks when
file -ITelling UTF-8 and UTF-16 from everything else, in a second, freeYou need to know which single-byte encoding it is
iconvConverting a file whose encoding you already know, in scripts and pipelinesYou are guessing, and each guess is another copy to inspect
CaxtonSeeing the text under each encoding, then editing and saving, or converting with Save AsThe conversion is a recurring batch job; script that one with iconv

Garbled text often turns up inside a database export; opening a SQL dump on a Mac covers reading those files whole.

Frequently asked questions

Why does my file show é or Рcharacters?

The free fix is in Terminal: file guesses the encoding and iconv converts the file to UTF-8. Caxton, a native Mac editor for text and data files, detects the encoding when a file opens, offers Reopen with Encoding when the guess was wrong, which brings the text back without touching the bytes, and can save the file as UTF-8.

How do I know which encoding a file is?

For UTF-8 and UTF-16 a tool can tell you: file -I in Terminal reports the charset. For single-byte encodings nothing can be certain, because every byte is valid in all of them, so the practical answer is where the file came from, then trying the likely candidates and reading the result. Caxton scores the candidates by how the decoded text reads and names its best guess in a banner, and you confirm it with your eyes.

Can I convert a file to UTF-8 on a Mac?

Yes, for free: iconv -f WINDOWS-1251 -t UTF-8 in.csv > out.csv, with the source encoding you identified after -f. In Caxton, reopen the file with the right encoding and use Save As, which writes UTF-8, with a line in the save panel to confirm it. File ▸ Save with Encoding… converts the file itself instead of writing a copy.

Does Caxton change the bytes when it reopens with a different encoding?

No. Reopening changes how the bytes are read, not the bytes. Caxton decodes them into a UTF-8 working copy and the file on disk is untouched until you save. Save writes it back in its original encoding, and Save As writes UTF-8.

The text is still in there. Download Caxton for Free and read it the right way.

Sources