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 convert a file to UTF-8 on a Mac.

An import form asks for UTF-8, a script stops at the first accented letter, or a colleague needs the file with a byte order mark. You already know the encoding has to change, and the job is to change it without changing the text. This guide shows how to check what a file is now, converts UTF-16, Windows-1252 and Shift JIS files with tools that are already on your Mac, and explains the byte order mark, the few bytes at the start of a file that some programs expect and others reject.

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

The short answer

To convert a file to UTF-8 on a Mac, run iconv in Terminal and name the encoding the file has now, as in iconv -f WINDOWS-1252 -t UTF-8 notes-1252.txt > notes-utf8.txt. iconv is already installed, the command writes a new file and leaves the original alone, and file -I gives a first guess at the current encoding. Caxton, a native Mac editor for text and data files, shows why it read a file in its encoding, converts the file with File ▸ Save with Encoding… after checking that every character can be written, and adds or removes the byte order mark from the Text menu.

How do I tell what encoding a file is in?

A plain text file is a run of bytes, and unless it starts with a byte order mark, nothing in it records which encoding turned the characters into those bytes. A tool can only look at the bytes and reason backwards, which works for some encodings and not for others. Every answer in this section is therefore a guess that you confirm by reading the text. If your file shows garbled characters and you do not yet know what is wrong with it, fix garbled text and the wrong encoding starts from that end.

The tool for a first look is file, which is included with macOS. Its manual page describes the -I option as one that makes it "output mime type strings rather than the more traditional human readable ones," and the answer ends with a charset. Three test files were built for this guide: a UTF-16 export that starts with a byte order mark, a Windows-1252 note with accented letters, curly quotes and a euro sign, and a Shift JIS memo.

file -I export-utf16.txt notes-1252.txt memo-sjis.txt
export-utf16.txt: text/plain; charset=utf-16le
notes-1252.txt:   text/plain; charset=unknown-8bit
memo-sjis.txt:    text/plain; charset=unknown-8bit

The first answer is right, and it rests on the byte order mark: a UTF-16 file without the mark was reported as binary in the same tests. The other two answers are unknown-8bit. The manual page explains the method, which is that character sets "can be distinguished by the different ranges and sequences of bytes that constitute printable text in each set." For these two files the byte ranges were enough to rule out ASCII and UTF-8, and not enough to name either encoding.

Two more files show file naming an encoding that is not the real one.

file -I plain-1252.txt orders.csv
plain-1252.txt: text/plain; charset=iso-8859-1
orders.csv:     text/csv; charset=us-ascii

plain-1252.txt is Windows-1252 again, this time with accented letters and nothing else unusual. Those bytes are the same letters in ISO-8859-1, and file names that standard. orders.csv is a 90 KB file of plain ASCII rows whose last row holds the word café in UTF-8. The manual page lists a limit named encoding, the "max number of bytes to scan for encoding evaluation," with a default of 65536, and the accented letter sits beyond it. Raising the limit changes the answer for the same file.

file -I -P encoding=200000 orders.csv
orders.csv: text/csv; charset=utf-8

So read the charset as a hint. An answer of utf-16le or utf-8 matched the real encoding in every test for this guide. An answer of us-ascii, iso-8859-1 or unknown-8bit leaves the question open, and the better clue is where the file came from. Microsoft describes code page 1252 as "the code page commonly used for English and other Western European languages," so Windows-1252 is the first candidate for a file in one of those languages that came from a Windows computer.

How do I convert a file to UTF-8 with iconv on a Mac?

iconv is included with macOS and does one job. In the words of its manual page, it "converts the codeset of file" from the encoding you name with -f to the one you name with -t, "and outputs the converted text on standard output." Standard output is the Terminal window, so each command below ends with > and the name of a new file. The original is never changed. Give the new file a different name. With the same name on both sides, the shell empties the file before iconv reads it, and in a test for this guide that left a file of zero bytes.

Convert UTF-16 to UTF-8

iconv -f UTF-16 -t UTF-8 export-utf16.txt > export-utf8.txt

Name the source UTF-16 when the file starts with a byte order mark. iconv reads the mark to learn the byte order and leaves it out of the result. The Unicode Consortium's FAQ gives the same rule for labels: "If you do use a BOM, tag the text as simply UTF-16." The names UTF-16LE and UTF-16BE are for a file that has no mark. Used on a file that has one, UTF-16LE treats the mark as a character and writes it at the start of the UTF-8 result.

iconv -f UTF-16LE -t UTF-8 export-utf16.txt | head -c 3 | hexdump -C
00000000  ef bb bf                                          |...|
00000003

Those three bytes are a UTF-8 byte order mark, and the section on the mark below explains when you want one. The opposite mistake does more damage. A little-endian file with no mark, converted with -f UTF-16, came out as unrelated characters with no error, because iconv read it in the other byte order. That matches the Unicode FAQ, in which the unmarked form "uses big-endian byte serialization by default." For such a file the right name is UTF-16LE.

Convert an ANSI or Windows-1252 file to UTF-8

ANSI is a Windows word for a family of encodings, and not the name of one. Microsoft's documentation states that Windows code pages are "commonly called" ANSI code pages and that "A Windows operating system always has one currently active Windows code page." An ANSI file is therefore in whichever code page the computer that wrote it was using. For English and Western European text that is usually code page 1252, which iconv calls WINDOWS-1252. Microsoft's list of code page identifiers has other ANSI entries for Central European, Cyrillic, Greek, Turkish and Japanese text, so ask where the file came from before you assume 1252.

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

The new file reads Café “Zoë” costs €12, and file -I reports charset=utf-8 for it. Do not name ISO-8859-1 for a Windows file, even when file suggested it. Microsoft notes that code page 1252 "is not exactly the same as ISO 8859-1," and the curly quotes and the euro sign are among the differences. Converted with -f ISO-8859-1, the same file kept its accented letters and turned both quotes and the euro sign into invisible control characters, again with no error.

Convert Shift JIS to UTF-8

iconv -f SHIFT_JIS -t UTF-8 memo-sjis.txt > memo-utf8.txt

The memo comes out as これは日本語のテキストです。 in UTF-8. A Shift JIS file written on Windows may need a different name. Microsoft's Japanese code page is number 932, and iconv lists it apart from Shift JIS under the name CP932. A test file holding the circled digit ① in code page 932 stopped iconv -f SHIFT_JIS with the message Illegal byte sequence, and it converted correctly with -f CP932. If SHIFT_JIS stops on your file, try CP932 next.

How to open a UTF-16 or Shift-JIS file on a Mac shows what else differs between SHIFT_JIS and CP932.

List the encodings iconv knows

The manual page gives iconv -l one line: it "Lists available codeset names." On macOS 27 the list is 215 lines long, and each line holds every name iconv accepts for one encoding. Filter it to find the name you need.

iconv -l | grep -E 'WINDOWS-1252|SHIFT_JIS|UTF-16LE'
CP1252 1252 MS-ANSI MSCP1252 WINDOWS-1252
CP932 932 CSWINDOWS31J MS932 MSCP932 SHIFT_JIS-MS SJIS-MS SJIS-OPEN SJIS-WIN WINDOWS-31J WINDOWS-932
SHIFT_JIS-2004 SHIFT_JISX0213
SHIFT_JIS CSSHIFTJIS MS_KANJI SHIFT-JIS SJIS
UTF-16 UNICODE UTF16 CSUNICODE CSUNICODE11 ISO-10646-UCS-2 UCS-2 UCS-2BE UNICODE-1-1 UNICODEBIG UTF-16BE UTF16BE UCS-2LE UNICODELITTLE UTF-16LE UTF16LE

Each line is one encoding under all of its names. MS-ANSI on the first line is another name for WINDOWS-1252, and it converted the Windows-1252 note identically. The last line is the one to watch: UTF-16, UTF-16LE and UTF-16BE share it, and they treat the byte order mark differently, as the UTF-16 example shows. The manual page adds a caution: "not all combinations of from_name and to_name are valid."

Convert every file in a folder

A job that covers many files is where a command-line tool is the better choice. This loop converts every .txt file in the current folder from Windows-1252 and writes the results into a new folder named utf8, leaving the originals where they are.

mkdir utf8
for f in *.txt
do
  iconv -f WINDOWS-1252 -t UTF-8 "$f" > "utf8/$f"
done

The loop assumes that every file in the folder has the same encoding, and iconv does not check that.

Read the result, because iconv trusts the name you give it

iconv has no way to know whether the source name is right. When the bytes are impossible in the encoding you named, it stops. The Windows-1252 note read as Shift JIS printed this message and wrote only the first three letters.

iconv: iconv(): Illegal byte sequence

When the bytes are possible in the wrong encoding, nothing stops. A Windows-1251 file holding the Russian words Привет, мир, converted with -f WINDOWS-1252, produced Ïðèâåò, ìèð and reported success. So open the new file and read a few lines that have accented or non-Latin characters before you delete the original.

Converting the encoding leaves the line endings as they are. If the file also has Windows line endings that the next program rejects, LF vs CRLF line endings covers converting them.

How do I change a file's encoding in TextEdit?

TextEdit is already installed on every Mac, and Apple's TextEdit User Guide documents two places where you choose an encoding for a plain text file. The first is the Open dialog, for one file. Apple's steps are to "choose File > Open, then select the file (don’t open it)," then "Click Show Options in the lower left of the window," and then "Click the Plain Text Encoding pop-up menu and choose an encoding." If the encoding you need is missing from the menu, "choose Customize Encodings List, then select the encodings to include." When you make no choice, the guide explains, "TextEdit uses its automatic text encoding to display documents."

The second place is in settings, for every file. Apple's steps are to "choose TextEdit > Settings, then click Open and Save." That pane has a section named Plain Text File Encoding, which the guide describes this way: "When opening or saving plain text files, choose the default text encoding options from the Plain Text File Encoding pop-up menus." The guide names one of those menus, the “Opening files” pop-up menu.

Those are the steps Apple documents. The guide describes the encoding for saving as a default only. It does not describe which encoding TextEdit writes when you save a file that was opened in a different one, and it does not mention the byte order mark. If you convert a file by opening it in TextEdit and saving it, check the result with file -I before you rely on it.

What is the byte order mark, and what is UTF-8 with BOM?

The Unicode Consortium's FAQ defines it: "A byte order mark (BOM) consists of the character code U+FEFF at the beginning of a data stream, where it can be used as a signature defining the byte order and encoding form, primarily of unmarked plain text files." UTF-16 stores text in units of two bytes, and the two bytes can be written in either order, so the mark tells a reader which order the file uses. The same character comes out as different bytes in each encoding, which is how the mark also identifies the encoding. The FAQ lists the bytes, and these are its entries for UTF-8 and UTF-16.

The file starts with these bytesThe encoding is
EF BB BFUTF-8
FF FEUTF-16, little-endian
FE FFUTF-16, big-endian

UTF-8 has only one byte order, so its mark has nothing to do with order. The FAQ's answer is that "UTF-8 always has the same byte order" and that an initial mark "is only used as a signature" for the encoding. UTF-8 with BOM is therefore the same encoding and the same text as UTF-8, with three extra bytes in front. It gets a name of its own only because the program that reads the file may care about those three bytes.

When does a program expect a byte order mark?

There is no general rule, only the rules of each program. The Unicode FAQ puts it this way: "A particular protocol (e.g. Microsoft conventions for .txt files) may require use of the BOM on certain Unicode data streams, such as files. When you need to conform to such a protocol, use a BOM." Microsoft's own documentation on the mark states that "For UTF-8, the byte order mark is optional, since the bytes may only be in one order," and that for UTF-16 and UTF-32 it "is required because those formats are sensitive to the byte ordering."

Excel is one program with a documented rule. Microsoft's support page on opening UTF-8 CSV files in Excel states: "You can open a CSV file encoded with UTF-8 normally if it was saved with BOM (Byte Order Mark)." For a file without the mark, the same page sends you to the import commands instead, "Data tab > Get Data > From file > Get Data from Text/CSV" or the option named Get Data From Text, which "opens the Text Import Wizard." So a UTF-8 CSV that other people will open in Excel the ordinary way should carry the mark.

Other programs want the opposite. The Unicode FAQ notes that "some recipients of UTF-8 encoded data do not expect a BOM," and that a mark "will interfere with any protocol or file format that expects specific ASCII characters at the beginning," with the first line of a Unix shell script as its example. If nothing tells you that the receiving program wants the mark, leave it out, and add it for a CSV that is going to Excel.

How do I check whether a file has a byte order mark?

Look at the first three bytes of the file.

head -c 3 notes-utf8-bom.txt | hexdump -C
00000000  ef bb bf                                          |...|
00000003

A file without the mark shows its first three characters there instead. For a plain text file, file without the -I option also reports the mark, as Unicode text, UTF-8 (with BOM) text. For a file it recognizes as CSV it answers only CSV text, and file -k adds the line about the mark. With -I it prints charset=utf-8 for both kinds of file, so that option cannot tell them apart.

How do I add a byte order mark to a UTF-8 file?

printf '\357\273\277' | cat - notes-utf8.txt > notes-utf8-bom.txt

printf writes the three bytes, given here as the octal numbers for EF, BB and BF, and cat puts the file after them. Only do this to a file that is already UTF-8 and has no mark, because the command adds the bytes without looking.

How do I remove a byte order mark from a UTF-8 file?

tail -c +4 notes-utf8-bom.txt > notes-utf8-plain.txt

In Apple's manual page for tail, a number with a leading plus sign is relative to the beginning of the input, and its example is that -c +2 starts at the second byte. Starting at the fourth byte therefore skips the three bytes of the mark. Check that the mark is there first, because tail drops the first three bytes whatever they are. iconv is not the tool for this job: converting the marked file from UTF-8 to UTF-8 left the mark in place.

For a command that removes the mark only when it is there, for a whole folder, and for checking that nothing else changed, see how to remove a BOM from a file on a Mac.

How do I convert a file to UTF-8 in Caxton?

Caxton keeps three jobs apart that are easy to confuse. Reading a file in a different encoding changes nothing on disk. Converting a file rewrites its bytes and keeps its text. Adding or removing the byte order mark changes only what the file starts with. Each job has its own command, and each step below names the tier it needs.

1. Open the file and read why it was read in its encoding

Drag the file onto Caxton, or choose File ▸ Open… and pick it. Caxton detects the encoding as the file opens and shows it at the right end of the status bar, beside the line ending. For the reasoning, choose View ▸ Show Inspector or press ⌥⌘I. The Encoding section of the Inspector has a row for the encoding, a Confidence row, a BOM row that reads Present or None, and a row named Why this encoding that gives the evidence in a sentence or two. These are the reasons it can give:

  • The file starts with a byte order mark, which settles the question.
  • All of the file is valid UTF-8, with the number of multi-byte sequences found, or every byte is plain ASCII, which UTF-8 reads unchanged.
  • The file has no mark and every other byte is zero, which is the pattern of UTF-16 text.
  • The file is not valid UTF-8 and its bytes fit one kind of text best, such as Western European, Cyrillic or Japanese text. The row also names the next closest encoding when there is one.
  • The file is not valid UTF-8 and no encoding Caxton knows fits its bytes well, so it may not be text at all.
  • You chose the encoding with File ▸ Reopen with Encoding, or a rule in Edit ▸ File Pattern Rules… set it for files with that name.
  • You saved the document in that encoding yourself.

For a large file the sentence also gives how much of the file was examined. Further down, the Invalid bytes row counts the bytes that are not valid in that encoding, and when there are any, its First and Next buttons jump to each one. Caxton shows such a byte as the replacement character, so look at each of them before you convert. All of this is reading, and it is Caxton Free at any file size.

2. Reopen the file first if the encoding is wrong

A conversion writes the text as Caxton read it. If the window shows the wrong characters, converting would store them in the file, so correct the reading first. File ▸ Reopen with Encoding lists 18 encodings, among them UTF-8 with and without a byte order mark, UTF-16 in both byte orders, Windows-1252, ISO-8859-1 and Shift-JIS. Choosing one reads the file from disk again in that encoding and changes nothing in the file. If the document has unsaved edits, Caxton asks before it discards them. Reopening is Caxton Free at any file size. Open a GB18030, GBK, or Big5 file on a Mac walks through this step for Chinese text.

3. Convert the file with Save with Encoding

Choose File ▸ Save with Encoding… to open its form. The Encoding menu holds the same 18 encodings and starts on the one the file has now. Below it is a checkbox named Start the file with a byte order mark, which is a choice for UTF-16 only. UTF-8 has two entries of its own, UTF-8 and UTF-8 (BOM), and the other encodings have no mark. Pick UTF-8 and click Save.

Caxton first makes sure that every character of the document can be written in the encoding you chose. Then it replaces the file in one step, with any unsaved edits included, and a banner confirms the new encoding and the size of the file. The window stays open on the same document, and its undo history and its bookmarks are kept. While a large file converts, the job is listed in the Operations panel, and cancelling it there leaves the file as it was.

Two things differ from iconv. Save with Encoding replaces the original and makes no copy, so duplicate the file in Finder first if you need the old one. And the plain save, ⌘S, keeps the encoding the file has: a file that opened as Windows-1252 is written back as Windows-1252. When a copy is what you want, File ▸ Save As… on a file that opened in an encoding other than UTF-8 writes a UTF-8 copy and leaves the original alone, and a line in the save panel confirms that before you click.

Save with Encoding and Save As are Caxton Free for a file up to 10 MB. A larger file needs Caxton Pro, which every download includes as a 7-day free trial of Caxton Pro with no credit card required. On Caxton Free a larger file still opens for reading, and the command is refused with a banner that gives the reason and a License button.

4. See what happens when a character cannot be written

UTF-8 and UTF-16 can write every character, so a conversion to either is never refused for this reason. The check matters in the other direction, when a UTF-8 file has to become Windows-1252 or Shift-JIS for an older system. Microsoft's documentation describes the risk: data converted that way "is subject to data loss, because a given code page might not be able to represent every character used in that particular Unicode data." Caxton does not write a file with characters missing. If the document holds characters the encoding has no bytes for, nothing is saved, and a banner counts them and names the first one in this form: Windows-1252 cannot write 4 characters of this document. The first is “✓” on line 2, and its code point is U+2713. Nothing was saved. The banner's Go to It button selects that character in the text, or its cell when the file is open as a table. Remove or replace the characters and run the command again.

5. Add or remove the byte order mark

Text ▸ Byte Order Mark holds Add and Remove. Only the one that applies is available, so the menu also shows whether the file has a mark. Either command changes what the next save writes and no byte of the text. A banner confirms that the next save starts the file with a byte order mark, or leaves it out, the document counts as edited, and ⌘Z takes the change back. The mark reaches the file when you save with ⌘S. Before it removes the mark from a UTF-16 file, Caxton asks, because the mark is how other programs know what that file is. Both items are unavailable for a file in an encoding that has no mark, such as Windows-1252. To convert and set the mark in one step, choose UTF-8 (BOM) or UTF-8 in Save with Encoding. Byte Order Mark is an edit like the others: Caxton Free for a file up to 10 MB, and Caxton Pro for a larger one.

Which way of converting fits which job?

The jobTools included with macOSIn Caxton
You know the encoding and want a UTF-8 copy.iconv writes one with a single command and leaves the original alone.File ▸ Save As… writes a UTF-8 copy of a file that opened in another encoding.
The file itself has to become UTF-8.Convert to a new name with iconv, read the result, then replace the original yourself.File ▸ Save with Encoding… checks every character and replaces the file in one step.
You are not sure what encoding the file has.file -I gives a first guess, and where the file came from narrows it.The Inspector gives the evidence for the encoding Caxton chose, and Reopen with Encoding tries another without changing the file.
A CSV for Excel needs the byte order mark.printf and cat put the three bytes in front.Text ▸ Byte Order Mark ▸ Add sets it for the next save.
A script has to lose the byte order mark.tail -c +4 drops the first three bytes.Text ▸ Byte Order Mark ▸ Remove leaves it out of the next save.
A whole folder has to be converted, or the job repeats on a schedule.A loop around iconv is the better tool here.Caxton converts the one file that is open in front of you.

Open the file, read why it was read in its encoding, and choose File ▸ Save with Encoding… to convert it once every character has been checked.

Download Caxton for Free

7-day free trial of Caxton Pro, every feature, no credit card required. After it, Caxton Free keeps opening, searching and filtering at any size, and you can edit and save files up to 10 MB as text.

Frequently asked questions

How do I know what encoding a file uses?

A plain text file does not record its encoding unless it starts with a byte order mark, so every tool makes a guess from the bytes. In Terminal, file -I prints that guess as a charset. In the tests for this guide it was right about a UTF-16 file that starts with a byte order mark and about UTF-8 files whose accented letters came early, and it answered us-ascii, iso-8859-1 or unknown-8bit for files that were really Windows-1252, Shift JIS, or UTF-8 with the first accented letter far into the file. Where the file came from is the better clue. In Caxton, the Why this encoding row of the Inspector gives the evidence for the encoding Caxton chose, and File ▸ Reopen with Encoding reads the file in another one without changing it.

What is the difference between UTF-8 and UTF-8 with BOM?

Three bytes. UTF-8 with BOM is ordinary UTF-8 with the bytes EF BB BF at the start of the file, which are the byte order mark, the character U+FEFF, written in UTF-8. The text after them is the same. The Unicode Consortium's FAQ describes the mark in UTF-8 as a signature only, and notes that some recipients of UTF-8 data do not expect it. Microsoft's support page for Excel states that a UTF-8 CSV opens normally if it was saved with the mark. So add the mark for a CSV that people will open in Excel, and leave it out when nothing tells you the receiving program wants it.

Will converting change my text?

Not when you name the right source encoding. A conversion to UTF-8 rewrites the bytes and keeps every character, because UTF-8 can write every character in Unicode. The text changes when the source is named wrong. In a test for this guide, iconv read a Windows-1251 file as Windows-1252 and wrote accented Latin letters where the Russian words had been, with no error. Line endings pass through unchanged, and the byte order mark is the one thing that may be added or dropped at the start of the file. Read a few lines of the result before you delete the original.

What if a character cannot be written in the new encoding?

That cannot happen when the new encoding is UTF-8, which can write every character in Unicode. It happens when you convert the other way, to an encoding such as Windows-1252 or Shift JIS that holds a limited set of characters. In a test for this guide, iconv converting a line with a check mark from UTF-8 to WINDOWS-1252 printed Illegal byte sequence, stopped at the check mark and left a file that ended there. With its -c option it dropped the check mark and kept the rest of the line. Caxton's File ▸ Save with Encoding… checks the whole document first. When a character cannot be written it saves nothing, a banner counts the characters and names the first one with its line and its code point, and the Go to It button selects it.

Sources