Skip to content
GEEKnodes

Computing

UTF-8 and Unicode: Why You See ñ or é Instead of Letters

What ASCII, Latin-1, Unicode and UTF-8 are, why ñ shows up as ñ or �, and how to fix garbled text in Excel CSV files, emails, web pages and databases.

You open a CSV and where it said “España” you see “España”. Or a website shows diamonds with question marks instead of accented letters. It is not a virus or a broken file: it is an encoding problem, and it has an exact explanation. Here you will see what ASCII, Latin-1, Unicode and UTF-8 are, why each symptom happens and how to fix it in the most common cases.

In short

  • A computer stores bytes; the encoding is the table that says which letter each byte is.
  • “ñ” appears when UTF-8 text is read as Latin-1 or Windows-1252. “�” appears in the opposite case.
  • The fix is almost always to declare UTF-8 everywhere: file, program, database and connection.

What an encoding is

A text file does not contain letters, it contains bytes: numbers from 0 to 255. To show an “A”, the program needs to know which number means “A”. That mapping is the encoding.

If the writer uses one table and the reader another, the bytes are the same but the letters are not. The file is not damaged; it is being misread.

ASCII, Latin-1 and Windows-1252

ASCII (1963) uses 7 bits: 128 characters. English letters, digits, punctuation and control characters. No ñ, no accents, no €.

To cover other languages, the eighth bit was used, giving another 128 slots. Each region filled them its own way:

  • ISO-8859-1 (Latin-1): Western European characters. The ñ is byte F1, the é is E9.
  • Windows-1252: almost the same as Latin-1 but uses the 80–9F range for characters like €, curly quotes or the long dash. For years it was Windows’ default in Western Europe and the Americas, and many programs still use it.

The problem with these tables is that they only hold 256 characters. A text could not mix Spanish, Greek and Russian, and each country had its own incompatible version.

Unicode and UTF-8 are not the same

Unicode is a catalog: it assigns a unique number, called a code point, to every character of every writing system, plus symbols and emoji. It is written as U+ followed by the hexadecimal number: ñ is U+00F1, é is U+00E9 and € is U+20AC.

Unicode assigns a number to each character and UTF-8 stores it in 1 to 4 bytes depending on the character
Unicode is the catalog of numbers; UTF-8 is the way to store them as bytes

UTF-8 is one way to store those numbers as bytes. It uses 1 to 4 bytes depending on the character:

Code point rangeBytes in UTF-8Examples
U+0000 – U+007F1English letters, digits (same as ASCII)
U+0080 – U+07FF2ñ, á, é, ç, Greek and Cyrillic letters
U+0800 – U+FFFF3€, most Chinese and Japanese characters
U+10000 – U+10FFFF4Emoji, rare symbols

Its big advantage is that any ASCII text is already valid UTF-8. That is why it won: today it is the encoding of the vast majority of websites.

There are also UTF-16 and UTF-32, which store the same Unicode code points in 2 and 4 byte units. Windows uses them internally, but UTF-8 is normal for files and the internet.

Same letter, different bytes

This table is the key to the whole article:

CharacterUnicodeLatin-1 / Windows-1252UTF-8
AU+00414141
ñU+00F1F1C3 B1
éU+00E9E9C3 A9
áU+00E1E1C3 A1
óU+00F3F3C3 B3
€U+20AC80 (Windows-1252 only)E2 82 AC

English letters match. Accented letters and ñ take one byte in Latin-1 and two in UTF-8.

Check it yourself on macOS or Linux:

printf 'ñ' | xxd
# 00000000: c3b1

Mojibake explained

Text mangled by the wrong encoding is called mojibake. There are two possible directions, and each leaves a different fingerprint.

Case 1: UTF-8 read as Windows-1252 (you get ñ)

The ñ in UTF-8 is two bytes: C3 B1. A program that thinks it is reading Windows-1252 interprets them one at a time:

  • C3 in Windows-1252 is Ã
  • B1 in Windows-1252 is ±
An ñ saved as UTF-8 is the bytes C3 B1; read one by one as Windows-1252 they show as à and ±
Two UTF-8 bytes read as two separate characters: that is how “ñ” is born

Result: “ñ”. That is why every error of this kind starts with Ã: common accented letters share the first byte C3.

You typedYou seeWhy
ññC3 B1
ééC3 A9
ááC3 A1
óóC3 B3
úúC3 BA
íÃC3 AD: the AD is an invisible hyphen
€â‚¬E2 82 AC

The í is the sneakiest: it looks like only a lone “Ô is left because the second byte does not show.

Case 2: Windows-1252 read as UTF-8 (you get �)

Now the other way round. The ñ in Windows-1252 is a single byte, F1. In UTF-8, an F1 byte announces the start of a 4-byte character, and the following ones do not fit. The program cannot decode it and puts the replacement character �, U+FFFD.

The ñ in Windows-1252 is byte F1; a UTF-8 reader cannot decode it and shows the replacement character
A lone byte that does not fit UTF-8 turns into the diamond with a question mark

Quick tip: if you see Ã, the file is UTF-8 and you are reading it wrong; if you see �, the file is not UTF-8 and you are reading it as if it were.

Case 3: double encoding

If already broken text (“ñ”) is saved again as UTF-8 and misread again, the damage grows: “ñ”. Each layer adds more characters. It usually happens when data is copied between systems several times.

Common cases and how to fix them

CSV in Excel

This is the most common one. A website or program exports a UTF-8 CSV and Excel for Windows opens it with the regional encoding, Windows-1252, if the file has no BOM.

Solutions:

  1. In Excel, use Data › From Text/CSV and choose 65001: Unicode (UTF-8) as the origin.
  2. If you generate the CSV yourself, save it with a BOM: Excel detects it and opens it correctly on double-click.
  3. When saving from Excel, choose CSV UTF-8 (Comma delimited), not plain “CSV”.

The BOM

The BOM (Byte Order Mark) is three bytes, EF BB BF, at the start of a UTF-8 file that mark its encoding. It helps Excel, but other programs treat it as text. Read as Windows-1252 it shows up as  at the beginning, and in scripts and config files it can cause errors on the first line.

Rule of thumb: BOM for CSVs opened in Excel; no BOM for code, configuration and web.

Emails

An email states its encoding in the Content-Type header. If the sending program declares one encoding and writes another, the recipient sees mojibake. If you generate emails from a website or script, make sure you send charset=UTF-8 and that the text really is UTF-8.

Web pages

The page must declare its encoding with the meta tag at the start of the head, and the server should send it in the HTTP header:

<meta charset="utf-8">
Content-Type: text/html; charset=utf-8

Databases

In MySQL and MariaDB, the utf8 character set is actually a version limited to 3 bytes: it does not support emoji. Use utf8mb4. Also, the connection between application and database has its own encoding; if it does not match, broken text gets stored.

CREATE DATABASE tienda CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
SET NAMES utf8mb4;

How to detect and convert files

To find out a file’s encoding:

file -i datos.csv   # Linux
file -I datos.csv   # macOS

To convert from Windows-1252 to UTF-8:

iconv -f WINDOWS-1252 -t UTF-8 datos.csv > datos-utf8.csv

In PowerShell 7 you can specify the encoding for reading and writing:

Get-Content .\datos.csv -Encoding windows-1252 | Set-Content .\datos-utf8.csv -Encoding utf8

If text is already broken with “ñ” (UTF-8 misread and saved), you can reverse it by undoing the wrong step:

roto = "España"
arreglado = roto.encode("cp1252").decode("utf-8")
print(arreglado)  # España

This only works if no information was lost along the way. With “�” there is no way back: the original character has already been replaced.

Common mistakes

  • Converting twice: converting to UTF-8 a file that was already UTF-8 creates double encoding. Check with file first.
  • Fixing by hand with find and replace: it works for ñ and fails for í and other cases. Better to reverse the encoding.
  • Trusting MySQL’s “utf8”: always use utf8mb4.
  • Assuming PowerShell’s encoding: Windows PowerShell 5.1 and PowerShell 7 do not write in the same encoding by default. Set -Encoding explicitly.
  • Declaring UTF-8 but saving something else: the label converts nothing; the file must really be saved as UTF-8.

Accessibility preferences

Quick profiles

Text and reading

Text size
Default
Line height
Default
Letter spacing
Default
Word spacing
Default

Colour

Motion and orientation

Cursor