Find a course

Module 1: Bits, Binary and DataLesson 3 of 1114 min

Representing Text, Images and Sound

Find out how ASCII and Unicode store text, how pixels and RGB store images, and how sampling turns sound into numbers, with real file-size math.

Step 1 of 13Learn

Everything is a number

A photo of your dog, a voice message and this sentence all end up as long rows of bits. The bits themselves don't carry meaning. Meaning comes from an agreed encoding: a rule that says how to turn a kind of information into numbers and back.

The byte 01000001 (decimal 65) could be the letter A in a text file, a fairly dark shade of red in an image, a quiet moment in an audio file, or part of an instruction in a program. Software uses the file type, headers and context to know which interpretation to apply. Open an image file in a plain text editor and you see gibberish because the editor applies the wrong encoding.

Three encodings cover most of what you see every day: character encodings for text, pixel grids for images and samples for sound. Each one involves a trade-off between quality and size, and the math behind those trade-offs explains why files are as big as they are.

Step 2 of 13Learn

Text: ASCII and Unicode

ASCII, created in the 1960s, assigns a number from 0 to 127 to each English letter, digit, punctuation mark and a few control codes. Uppercase A is 65, B is 66, and so on to Z at 90. Lowercase a is 97, exactly 32 more than A. The digit character 0 is 48, which is why the text “7” and the number 7 are not the same thing to a computer.

ASCII fits in 7 bits, but it has no room for é, ñ, Ж, 中 or emoji. Unicode solves this by giving every character in every writing system a unique number called a code point, written like U+0041 for A. Unicode now covers well over 150,000 characters.

Code points still have to be stored as bytes. The most common way is UTF-8, which uses 1 byte for ASCII characters, 2 bytes for most European and Middle Eastern letters, 3 bytes for most Chinese, Japanese and Korean characters, and 4 bytes for emoji and rarer symbols. Because plain English text in UTF-8 is byte-for-byte identical to ASCII, older files kept working, which is a big reason UTF-8 became the standard encoding of the web.

When text shows up as strange symbols, the usual cause is a program reading UTF-8 bytes as if they were a different encoding.

Step 3 of 13Practice · Fill in the blanks

Fill in the blanks

In ASCII, uppercase A is stored as , so uppercase C is stored as . Lowercase letters are 32 higher, so lowercase c is .

Step 4 of 13Note

Step 5 of 13Learn

Images: pixels and RGB

A digital image is a grid of tiny squares called pixels. Each pixel stores a color as numbers. The most common scheme is RGB: one value each for red, green and blue light, mixed together the way a screen does it. With 8 bits per channel, each value runs from 0 to 255, so one pixel needs 24 bits (3 bytes), and there are 2563 ≈ 16.7 million possible colors.

  • (255, 0, 0) is pure red.
  • (255, 255, 255) is white: all three at full strength.
  • (0, 0, 0) is black.
  • (255, 255, 0) is yellow, because red and green light mix to yellow.

The size of an uncompressed image is simple multiplication:

bytes = width × height × bytes per pixel

A Full HD frame of 1920 × 1080 pixels at 3 bytes per pixel is 1920 × 1080 × 3 = 6,220,800 bytes, about 6.2 MB. A 12-megapixel phone photo would be about 36 MB uncompressed. Real photo files are much smaller because formats such as JPEG use compression, which you'll meet in a moment.

Step 6 of 13Try the calculator

Uncompressed image size

Try a phone screen, a 4K frame (3840 × 2160) or a tiny icon. Use 3 bytes per pixel for RGB, 4 if there is a transparency channel.

Pixels
2,073,600
Size in bytes
6,220,800
Size in MB
6.22 MB

Step 7 of 13Practice · Work it out

A small game sprite sheet is 400 × 250 pixels with 4 bytes per pixel (RGB plus transparency). What is its uncompressed size in kilobytes (1 KB = 1,000 bytes)?

Step 8 of 13Learn

Sound: sampling

Sound is a smooth wave of air pressure. To store it, a microphone turns the wave into a changing voltage, and an analog-to-digital converter measures that voltage many times per second. Each measurement is a sample.

Two settings control quality:

  • Sample rate: how many samples per second. CD audio uses 44,100 samples per second (44.1 kHz). To capture a frequency, you need at least two samples per cycle, so 44.1 kHz can represent sounds up to about 22 kHz, just above the limit of human hearing.
  • Bit depth: how many bits per sample. CD audio uses 16 bits, giving 65,536 possible levels, so quiet details aren't lost in rounding.

The data rate is sample rate × bit depth × channels. For stereo CD audio: 44,100 × 16 × 2 = 1,411,200 bits per second, about 176 KB every second, or roughly 10.6 MB per minute.

A voice call can use a much lower sample rate because speech doesn't need high frequencies. That's one reason phone calls sound thinner than music.

Step 9 of 13Explore

Sampling and bit depth

The smooth wave is stored as steps. Fewer levels means a rougher copy of the sound. Slide the levels up and watch the steps shrink.

-1-0.500.51 0246
Total levels
7
Step size
0.333

Step 10 of 13Practice · Work it out

A voice recorder samples at 16,000 samples per second, 16 bits per sample, in mono (1 channel). How many kilobytes does one second of uncompressed audio take (1 KB = 1,000 bytes)?

Step 11 of 13Compare

Lossless vs. lossy compression

AspectLosslessLossy
IdeaFind patterns and store them more briefly, then rebuild the exact originalThrow away detail people are unlikely to notice
Original recoverable?Yes, bit for bitNo, some data is gone for good
Typical size savingOften 2 to 3 times smaller for text and simple graphicsOften 10 times smaller or more for photos and music
ExamplesZIP, PNG, FLACJPEG, MP3, AAC, most video formats
Use it forDocuments, code, screenshots, archivesPhotos, streaming music and video

Step 12 of 13Note

Step 13 of 13Practice · Select all that apply

Which of these would make an uncompressed audio file larger? Select all that apply.

Select all that apply.