Encoding

Character Encoding Explained: ASCII, Unicode and UTF-8

Understand how computers turn characters into bytes, and how ASCII, Unicode and UTF-8 relate, plus how to avoid garbled text and mojibake in your code.

On this page
  1. ASCII: the early standard
  2. The era of code pages
  3. Unicode: one number for every character
  4. UTF-8: the practical answer
  5. Why garbled text appears
  6. Characters versus bytes
  7. Practical rules
  8. Frequently Asked Questions

Computers store everything as numbers, so text has to be translated into numbers before it can be saved or sent. A character encoding is the set of rules for that translation. When two programs disagree about the rules, text turns into nonsense: question marks, empty boxes or strange sequences such as é in place of é. Understanding the basics of encoding explains those problems and shows how to prevent them.

This article moves through the three names you meet most often: ASCII, Unicode and UTF-8. They are related, but they are not the same kind of thing.

ASCII: the early standard

ASCII, the American Standard Code for Information Interchange, assigns numbers to 128 characters. They include the English letters in upper and lower case, the digits 0 to 9, common punctuation and a set of control codes such as newline and tab. For instance, the capital letter A is 65 and the lowercase a is 97.

Seven bits are enough for 128 values, so ASCII fits comfortably inside a single byte. That made it simple and efficient. It also made it far too small. There is no room for accented letters, other alphabets, or the thousands of characters used in Chinese, Japanese and Korean.

The era of code pages

Since a byte has 256 possible values and ASCII used only the first 128, vendors used the upper half for extra characters. The trouble was that everybody chose differently. One code page filled that space with Western European accents, another with Cyrillic letters, another with Greek. The same byte value could mean different characters depending on which code page the reader assumed.

A file written in one code page and opened in another showed the wrong characters, and mixing languages in one document was almost impossible. This confusion is what Unicode was created to end.

Unicode: one number for every character

Unicode is a universal catalogue. It gives each character a unique number, called a code point, regardless of platform, program or language. Code points are written in the form U+0041 for A, or U+00E9 for é. The catalogue covers a huge range of scripts, symbols, mathematical signs and emoji, and it keeps growing.

The key point is that Unicode itself is not an encoding. It defines which number belongs to which character. A separate question remains: how do you store those numbers as bytes? Several answers exist, called Unicode transformation formats, and the most important is UTF-8.

UTF-8: the practical answer

UTF-8 stores each code point using one to four bytes. Its design has some clever properties:

  • The first 128 code points use one byte, and they are identical to ASCII. Any plain ASCII file is already valid UTF-8.
  • Characters from other alphabets use two or three bytes, and rarer ones, including many emoji, use four.
  • The byte patterns make it easy to find where a character starts, so a program can recover if it lands in the middle of one.
  • It has no problem with byte order, unlike some other formats.

For example, A is the single byte 0x41, while é takes two bytes, 0xC3 and 0xA9. Because of its compatibility and efficiency for English and code, UTF-8 became the dominant encoding on the web and in modern software.

Other formats exist. UTF-16 uses two or four bytes per character and is used internally by some systems. UTF-32 uses four bytes for every character, which is simple but wasteful.

Why garbled text appears

Garbled text, called mojibake, appears when bytes are decoded with the wrong encoding. A classic case is a UTF-8 file read as if it were an older Western code page. The two bytes for é are shown as two separate characters, producing é.

Another symptom is the replacement character, a diamond with a question mark, which a decoder shows when it meets bytes that are invalid in the encoding it expected. Question marks inside text often mean data was converted to an encoding that could not represent the character, and the original information is lost permanently.

Characters versus bytes

A frequent source of bugs is mixing up characters and bytes. The text café has four characters but occupies five bytes in UTF-8. If a program counts bytes when it should count characters, it may cut a string in the middle of a character and corrupt it.

Some visible characters are built from several code points, such as a letter plus a combining accent, or many emoji made by joining several together. So even the idea of "one character" can be subtle. Use the string functions of your language that are designed for Unicode, and avoid slicing text by byte positions.

Practical rules

  1. Use UTF-8 everywhere you can: source files, databases, text files, web pages and APIs.
  2. Declare the encoding. Web pages should state it in the document, and servers in the response header. Many databases need it set when you create them.
  3. Decode at the edges. Convert bytes to text when reading input, work with text inside your program, and encode again when writing output.
  4. Be explicit in code. In Python, write open("file.txt", encoding="utf-8") rather than relying on a system default, which can vary by platform.
  5. Beware of the byte order mark. Some Windows tools add an invisible marker at the start of a UTF-8 file, which can confuse programs that do not expect it.
  6. Test with non English text, including accents, non Latin scripts and emoji, early in a project.

Frequently Asked Questions

Is UTF-8 the same as Unicode?

No. Unicode is the catalogue of characters and their code points. UTF-8 is one way to store those code points as bytes. Other encodings, such as UTF-16, store the same Unicode characters in different byte patterns.

Why does my text show strange characters like é?

Almost always, UTF-8 bytes were read using a different encoding. Find where the text is decoded, and make sure that step uses UTF-8. Be aware that if the wrong text was saved over the original, you may need the source data to repair it.

How many bytes does a character use in UTF-8?

Between one and four. Basic English letters and digits use one, many European and Middle Eastern letters use two, most East Asian characters use three, and some symbols and emoji use four.