← All lessons
How computers work · Lesson 2 of 7

Bytes, hex and text

Eight bits make a byte, which can hold 256 different values. Bytes are the unit memory and files are measured in: a kilobyte (KB) is 1,000 bytes, a megabyte (MB) a million, a gigabyte (GB) a billion. (You'll also see KiB and MiB, which use 1,024 instead of 1,000.)

Long rows of bits are hard to read, so programmers often write them in hexadecimal (base 16). Hex uses the digits 0 to 9 and then A to F for 10 to 15. One hex digit stands for exactly four bits, so a byte is always two hex digits: 1111 1111 is FF (255), and 0010 1010 is 2A (42).

Colours on the web are written this way: #FF8800 is three bytes, red FF (255), green 88 (136) and blue 00 (0): orange.

Text is stored as numbers too. Every character is given a number called its code point. The early standard, ASCII, covered English letters, digits and symbols in 128 codes: A is 65, B 66, a 97, the digit 0 is 48 and a space is 32. Unicode extends this to every writing system and emoji, about 150,000 characters so far.

UTF-8 is the way almost every web page stores Unicode: ASCII characters take one byte, and others take two to four. Python's ord() gives a character's code point and chr() goes back.

print(ord("A"), ord("a"), chr(66))
print(len("é".encode("utf-8")), len("🦆".encode("utf-8")))
Prints
65 97 B
2 4

Practice

How many bits are in a byte?

More info

Quick reference

Byte8 bits: 256 values (0–255).
KB, MB, GBThousand, million, billion bytes.
HexBase 16 (0–9, A–F). Two hex digits per byte.
ASCII128 codes for English text: A = 65, a = 97.
Unicode / UTF-8Every character; stored in 1 to 4 bytes.

The other computer science courses