Character Encoding: ASCII, Unicode and UTF-8

Every letter, digit and symbol you type has to become numbers before a computer can store or send it, because memory holds only bits. A character encoding is the agreement that maps characters to numbers and those numbers to the bytes that are actually stored. You rely on it when a file opens full of strange symbols, when you estimate how much space a message takes, and when a program sorts or compares text.

Characters, code points and bytes

Encoding works in two layers. A character set assigns a code point, a whole number identifying a character. An encoding form then decides which bytes represent that number. Keep the layers apart: a character's Unicode code point never changes, but its bytes depend on the encoding form.


A fixed-width code with nn bits can tell apart


N=2nN = 2^{n}


different characters.


Example: A 7-bit code has 27 = 128 patterns, an 8-bit code has 256 and a 16-bit code has 65,536. To find how many bits a set of 300 characters needs, look for the smallest nn with 2n≥3002^n \ge 300. Since 28 = 256 is too few and 29 = 512 is enough, n=9n = 9.

ASCII and its limits

ASCII is a 7-bit code, so its values run from 0 to 127. Values 0 to 31 and 127 are control characters, such as tab (9) and line feed (10); 32 is the space, and the remaining values are printable punctuation, digits and letters. Digits and letters are numbered consecutively, so three anchors let you work out most codes:


Character

Decimal

Hexadecimal

7-bit binary

digit 0

48

30

0110000

A

65

41

1000001

a

97

61

1100001


So "D" is 65 + 3 = 68, and the digit "5" is 48 + 5 = 53. Each lowercase letter is exactly 32 above its uppercase partner. Because 32 = 25, the two cases differ in a single bit, which makes case conversion cheap.


ASCII has no room for é, ñ, π or Chinese characters. Many 8-bit code pages used the spare values 128 to 255, but each assigned them differently: the byte E9 is é in Latin-1 and й in the Cyrillic code page Windows-1251.



Unicode and UTF-8

Unicode is one character set for nearly every writing system, plus mathematical symbols and emoji. Its code points are written as U+ followed by hexadecimal digits: U+0041 is A, U+03C0 is π and U+4E2D is 中. They run from U+0000 to U+10FFFF, which is 1,114,112 values and needs 21 bits. The first 128 Unicode code points match ASCII exactly.


The range U+D800 to U+DFFF is reserved for surrogate code points. These are excluded from the code points encodable in UTF-8, UTF-16 and UTF-32. All other code points are Unicode scalar values, called encodable code points here.


UTF-8 stores each encodable code point in one to four bytes. The leading bits of each byte show its role, and the x positions carry the code point:


Encodable code point range

Bytes

Bit pattern

Payload bits

U+0000 to U+007F

1

0xxxxxxx

7

U+0080 to U+07FF

2

110xxxxx 10xxxxxx

11

U+0800 to U+D7FF; U+E000 to U+FFFF

3

1110xxxx 10xxxxxx 10xxxxxx

16

U+10000 to U+10FFFF

4

11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

21


To encode an encodable code point, find its row, write it in binary padded to the payload width, and fill the x positions from left to right.


Example: The pound sign £ is U+00A3, binary 1010 0011, which falls in the two-byte row. Padded to 11 bits it is 000 1010 0011. The first five bits, 00010, follow the marker 110 and the last six, 100011, follow the marker 10, giving 11000010 10100011, or C2 A3 in hexadecimal.



Other encoding forms trade space differently. UTF-16 uses 2 bytes for encodable code points up to U+FFFF and 4 bytes, a surrogate pair, above that. UTF-32 uses 4 bytes for every encodable code point, simplifying indexing by code point but using more space for mostly English text. One displayed character can contain multiple code points, such as e followed by a combining acute accent. Fixed width therefore does not provide direct indexing of displayed characters.

What to remember

  • A character encoding maps characters to code points and code points to bytes.
  • An nn-bit fixed-width code has N=2nN = 2^n patterns; ASCII uses 7 bits for 128 codes.
  • ASCII anchors: digit 0 = 48, A = 65, a = 97; lowercase is uppercase plus 32.
  • Unicode code points run from U+0000 to U+10FFFF; UTF encodings exclude U+D800 to U+DFFF.
  • For encodable code points, UTF-8 uses 1–4 bytes, UTF-16 uses 2 or 4 bytes, and UTF-32 uses 4 bytes.
  • A displayed character may contain multiple code points; UTF-32 simplifies indexing by code point only.

Example: Encoding a café price label in UTF-8

A café's display board stores the label "Café €4" as UTF-8 text. The label has seven characters: C, a, f, é, a space, € and the digit 4. The two non-ASCII characters have code points é = U+00E9 and € = U+20AC. In ASCII, "A" is 65 (hex 41), "a" is 97 (hex 61), the digit 0 is 48 (hex 30) and the space is 32 (hex 20).


a) Give the UTF-8 bytes, in hexadecimal, for the five ASCII characters C, a, f, space and 4.


Code points U+0000 to U+007F take one byte in UTF-8, and that byte equals the ASCII code.

  • C is two letters after A, so its code is 65 + 2 = ________, which is 4 × 16 + 3, or 43 in hex.
  • The letter a is given as 61 in hex.
  • The letter f is five letters after a, so its code is 97 + 5 = ________, which is 6 × 16 + 6, or 66 in hex.
  • The space is 20 in hex.
  • The digit 4 is 48 + 4 = 52, which is 3 × 16 + 4, or 34 in hex.

The five ASCII characters are stored as 43, 61, 66, 20 and 34, one byte each, so they use ________ bytes in total.


b) Encode é (U+00E9) in UTF-8 and give the bytes in hexadecimal.


The code point 00E9 is 233 in decimal, which lies between U+0080 and U+07FF, so it needs ________ bytes with the pattern 110xxxxx 10xxxxxx and 11 payload bits.


In binary, E9 is 1110 1001. Padded to 11 bits it becomes 000 1110 1001.


Split the 11 bits into the first five and the last six: ________ and ________.


Fill the pattern: 110 followed by 00011 gives 11000011, which is C3; 10 followed by 101001 gives 10101001, which is A9.


The character é is stored as the two bytes C3 A9.


c) Encode € (U+20AC) in UTF-8 and give the bytes in hexadecimal.


U+20AC lies between U+0800 and U+FFFF, so it needs three bytes with the pattern 1110xxxx 10xxxxxx 10xxxxxx and 16 payload bits.


Each hex digit becomes four bits: 2 is 0010, 0 is 0000, A is 1010 and C is 1100, so 20AC is 0010 0000 1010 1100. No padding is needed because the code point already fills 16 bits.


Split the bits into groups of four, six and six: 0010, ________ and 101100.


Fill the pattern: 1110 followed by 0010 gives 11100010, which is E2; 10 followed by 000010 gives 10000010, which is 82; 10 followed by 101100 gives 10101100, which is AC.


The character € is stored as the three bytes E2 82 AC.


d) How many bytes does the whole label occupy in UTF-8, and what percentage of its UTF-32 size is that?


Add the bytes from the earlier parts: 5 bytes for the ASCII characters, 2 for é and 3 for €.


5+2+3=10 bytes5 + 2 + 3 = 10\ \text{bytes}


In order, the stored bytes are 43 61 66 C3 A9 20 E2 82 AC 34.


UTF-32 uses 4 bytes for every character, so the seven characters need 7 × 4 = ________ bytes.


1028=0.357=35.7%\frac{10}{28} = 0.357 = 35.7\%


The label takes 10 bytes in UTF-8, about 35.7% of its UTF-32 size, because most of its characters are ASCII and need only one byte each.

Match each term used to describe how computers store text with the description that defines it. Every description is used exactly once.

Match each term
A.

A rule that maps characters to numbers and those numbers to the bytes actually stored

B.

A 7-bit code with 128 values covering English letters, digits, punctuation and control codes

C.

A single character set covering nearly every writing system, with values up to U+10FFFF

D.

A variable-length form that stores each encodable Unicode code point in one to four bytes and leaves ASCII text unchanged

E.

The number a character set assigns to one character, written as U+ and hexadecimal digits in Unicode

F.

A byte of the form 10xxxxxx that follows the lead byte of a multi-byte sequence

G.

A fixed-width form that stores every encodable Unicode code point in exactly four bytes

Continuation byte

Unicode

UTF-32

Code point

ASCII

Character encoding

UTF-8

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A laboratory archives text logs containing measurements and spaces. Complete each compression category using lossless or lossy.


The archive's approved method allows a decoder to reconstruct every original byte, including every space. This is compression.


A proposed alternative permanently deletes every space without storing enough information to recover its original position. This is compression.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

In ASCII, uppercase A has decimal code 65, lowercase a has code 97 and the digit 0 has code 48. Codes are consecutive within each group: uppercase letters, lowercase letters and digits. Fill in the decimal and hexadecimal ASCII codes for each character in the table. Write each hexadecimal code as two hex digits with no prefix, in the same style as 41 for uppercase A.

CharacterDecimal codeHexadecimal code
uppercase M
uppercase Z
lowercase m
digit 7

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice