Differences between utf8 and latin1
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UTF-8 is a variable-width encoding that supports every Unicode character using 1 to 4 bytes per character. Latin-1 (ISO-8859-1) is a fixed-width single-byte encoding that supports only 256 characters, covering ASCII and Western European languages. The key practical difference is that UTF-8 can represent any language on earth while Latin-1 cannot, but Latin-1 uses less storage for Western European text and has simpler string indexing since every character is exactly one byte.
For any new project, UTF-8 is the correct default. Latin-1 still matters when you work with legacy systems, older databases, or data pipelines that were built before Unicode became standard.
How Each Encoding Works
UTF-8 Byte Sequences
UTF-8 encodes characters using a variable number of bytes. The first byte's leading bits indicate how many bytes the character uses.
Characters in the ASCII range (U+0000 to U+007F) use exactly one byte, identical to ASCII. This backward compatibility is one of the reasons UTF-8 became the dominant encoding on the web.
Latin-1 Byte Values
Latin-1 maps byte values 0x00 through 0xFF directly to the first 256 Unicode code points. Every character is exactly one byte.
This direct byte-to-character mapping makes Latin-1 simple to work with programmatically. String length equals byte length, and random access by character index is O(1).
Side-by-Side Comparison
| Feature | UTF-8 | Latin-1 (ISO-8859-1) |
| Bytes per character | 1 to 4 | Always 1 |
| Total characters supported | Over 1.1 million (full Unicode) | 256 |
| ASCII compatible | Yes (first 128 bytes identical) | Yes (first 128 bytes identical) |
| Western European languages | Yes | Yes |
| CJK, Arabic, Hebrew, etc. | Yes | No |
| Emoji support | Yes | No |
| String indexing | O(n) for character index | O(1) for character index |
| Storage for English text | Same as ASCII (1 byte/char) | Same as ASCII (1 byte/char) |
| Storage for European accented text | 2 bytes per accented character | 1 byte per accented character |
| Web adoption | Over 98% of websites | Legacy use only |
Storage and Performance Implications
The storage difference depends entirely on the content. For ASCII-only text (English without special characters), UTF-8 and Latin-1 use the exact same number of bytes. For text heavy in accented characters (French, German, Spanish), UTF-8 uses roughly 10-20% more space because accented characters become two bytes instead of one.
For CJK text, UTF-8 uses 3 bytes per character, which makes it roughly three times the size of a hypothetical single-byte encoding. But since Latin-1 cannot represent CJK at all, this comparison only matters when choosing between UTF-8 and other multi-byte encodings like UTF-16.
Database Considerations
MySQL
MySQL has three relevant character sets: latin1, utf8 (which supports only up to 3-byte characters, missing emoji and some CJK), and utf8mb4 (full 4-byte UTF-8).
The MySQL utf8 type is a common trap. It only supports characters up to 3 bytes, which means it silently truncates emoji and some CJK characters. Always use utf8mb4 for true UTF-8 support.
PostgreSQL
PostgreSQL uses UTF-8 by default and does not have the 3-byte limitation. Latin-1 (LATIN1) is available as a database encoding but is rarely used in modern deployments.
Detecting and Converting Encodings
Python
Command Line
Java
Web and HTTP Headers
Browsers and servers communicate encoding expectations through HTTP headers and HTML meta tags.
The HTTP Content-Type header can also specify encoding:
As of 2024, over 98% of websites use UTF-8. The W3C and WHATWG both recommend UTF-8 as the default encoding for all new content.
Common Pitfalls
Treating Latin-1 bytes as UTF-8 without conversion. Latin-1 bytes in the range 0x80 to 0xFF are invalid as standalone UTF-8 bytes. Reading a Latin-1 file as UTF-8 produces decoding errors or mojibake (garbled text like "cafe" instead of "cafe").
Using MySQL utf8 instead of utf8mb4. MySQL's utf8 encoding only supports 3-byte characters. Emoji, musical notation, and some CJK characters will be silently truncated or rejected. Always use utf8mb4 for proper UTF-8 support.
Assuming string length equals byte length in UTF-8. In Latin-1, len(bytes) == len(characters) always holds. In UTF-8, a 10-character string might be anywhere from 10 to 40 bytes. Code that allocates buffers based on character count will underallocate for non-ASCII UTF-8 text.
Double-encoding UTF-8. This happens when UTF-8 text is incorrectly treated as Latin-1 and then re-encoded to UTF-8. The result is byte sequences like 0xC3 0x83 0xC2 0xA9 for what should be a single accented character. If you see strings like "Renée" instead of "Renee", double-encoding is the likely cause.
Forgetting the BOM (Byte Order Mark). Some Windows tools prepend a UTF-8 BOM (0xEF 0xBB 0xBF) to files. This invisible prefix can break parsers, shell scripts, and CSV readers that do not expect it. Latin-1 files never have a BOM.
Summary
UTF-8 is the universal encoding for modern software, supporting every Unicode character with efficient storage for ASCII-heavy content. Latin-1 is a single-byte encoding limited to 256 characters, still found in legacy databases and file formats. For new projects, always choose UTF-8 (and utf8mb4 in MySQL specifically). When working with legacy Latin-1 data, use explicit conversion tools like iconv or language-level decode/encode functions, and watch for double-encoding artifacts. The most common real-world problem is not choosing between the two encodings but accidentally mixing them in the same data pipeline.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.