Computer Science › Section B · Data storage › Topic B.4

Representing text, images, and sound as bits

A text message, a selfie and a song all end up as nothing but 0s and 1s. What turns those bits back into words, pictures and music is an agreed code, and the choice of code decides how much storage each one needs.

Everything needs a code

Bits have no built-in meaning, so storing anything requires an encoding: an agreement about which pattern stands for what. The pattern 01000001 can mean the number 65, the letter A or the brightness of one pixel, depending on the code in use. When sender and receiver disagree, you get garbled symbols or a file that will not open. This topic covers the main codes for text, images and sound, and the sizes they produce.

ASCII: one byte per character

Text is stored by giving each symbol its own bit pattern and storing the patterns one after another. Early machines each used their own codes, which made exchanging data painful, until ASCII, a US standard adopted in the 1960s, became the common code for English text. It uses 7 bits, so it has \(2^7 = 128\) patterns: upper- and lowercase letters, digits, punctuation, and control codes such as tab and line feed. Stored in a byte with a leading 0, the letter A is 01000001 (65) and a is 01100001 (97). The extra 128 patterns made by a leading 1 were later used by various national extensions.

Unicode and UTF-8

A 256-symbol table cannot hold the world's writing systems, and national extensions could not be mixed in one document. Unicode solves both problems by giving every character a single number, its code point, from a space of 1,114,112 possible values that needs up to 21 bits. Version 16.0 (2024) defined 154,998 characters, from Arabic and Tamil script to emoji. UTF-8, used by most web pages, stores each code point in 1 to 4 bytes. ASCII characters keep their one-byte ASCII patterns, so old English text is already valid UTF-8; accented Latin, Greek and Hebrew letters take 2 bytes; most Chinese and Japanese characters take 3; and most emoji take 4.

Worked example: encoding text

Encode Go 4! in ASCII and UTF-8. 1. Look up each code: G is 71, o is 111, the space is 32, the digit 4 is 52 and ! is 33. 2. Write each in 8 bits: 01000111 01101111 00100000 00110100 00100001. 3. Count: 5 characters, 5 bytes, 40 bits. Note that the digit 4 is stored as 52, not as the number 4. 4. All five are ASCII characters, so UTF-8 gives the same 5 bytes. 5. Compare Café: C, a and f take 1 byte each, but é takes 2 bytes (0xC3 0xA9), so 4 characters need 5 bytes.

Text files and numbers

A plain text file, the kind a simple text editor handles, contains nothing but character codes. A word processor file also stores formatting such as fonts and alignment, so it has extra structure around the text. Most files also carry metadata, data about the data such as a photo's camera settings, and a file format fixes how all of it is laid out. Character codes are also a wasteful way to store numbers. Written as the characters 2, 0 and 0, the value 200 needs 3 bytes in ASCII, but in binary notation it fits in a single byte, 11001000. One byte in binary covers every whole number from 0 to 255. The topics "The binary system" and "Storing integers: two's complement and excess notation" develop number codes properly.

Images as bit maps

One way to store a picture is to divide it into a grid of tiny dots called pixels, short for picture elements, and encode each pixel. The result is a bit map. In a pure black-and-white image, one bit per pixel is enough. A greyscale photo usually uses 8 bits per pixel, giving 256 shades. Colour images commonly use RGB encoding: one byte each for the red, green and blue components of light, so 24 bits per pixel and \(2^{24}\), about 16.7 million, possible colours. Bit maps match the way screens and printers work, which makes them easy to display.

Brightness, colour and scaling

An alternative to RGB stores each pixel as one brightness component, the luminance, plus two colour-difference components called blue and red chrominance. Luminance is a weighted sum of red, green and blue in which green counts most, matching the eye's sensitivity. The idea came from colour television, since black-and-white sets could simply show the luminance. Bit maps have one weakness: enlarging them only makes the pixels bigger, so images turn blocky. Vector graphics avoid this by storing shapes as geometric descriptions, lines, curves and fills, which are redrawn sharply at any size. Scalable fonts and SVG files work this way.

Worked example: the size of an image

How much storage does an uncompressed 1280 × 720 photo need? 1. Count the pixels: 1,280 × 720 = 921,600 pixels. 2. In 24-bit RGB each pixel needs 3 bytes: 921,600 × 3 = 2,764,800 bytes, about 2.76 MB. 3. As an 8-bit greyscale image it needs 921,600 bytes, one third as much. 4. As a 1-bit black-and-white image it needs 921,600 ÷ 8 = 115,200 bytes. 5. Check: the sizes are in the ratio 24 : 8 : 1, matching the bits per pixel. Real photo files are usually much smaller because they are compressed, as the topic "Data compression" explains.

Sampling sound

Sound is a continuous wave of air pressure. To store it, a computer measures the wave's amplitude at regular intervals and records each measurement as a number. Samples per second is the sample rate; bits per sample is the bit depth. Digital telephone systems have long used 8,000 samples per second at 8 bits, enough for speech. Music CDs use 44,100 samples per second at 16 bits for each of two stereo channels. By the sampling theorem, the rate must exceed twice the highest frequency captured; human hearing reaches about 20,000 Hz. MIDI instead stores instructions for a synthesiser, such as which instrument plays which note, not the sound itself.

Worked example: the size of a song

How much storage does a 3-minute song need at CD quality, uncompressed? 1. Duration: 3 × 60 = 180 seconds. 2. Bits per second: 44,100 samples × 16 bits × 2 channels = 1,411,200 bits per second. 3. Total bits: 1,411,200 × 180 = 254,016,000 bits. 4. Bytes: 254,016,000 ÷ 8 = 31,752,000 bytes, about 31.8 MB. 5. Compare with MIDI: starting a note is a 3-byte message and stopping it another 3, so a MIDI version might need only kilobytes. The catch: like sheet music, it sounds different on different synthesisers.

Common mistakes to avoid

First, assuming every character takes one byte: in UTF-8 a character takes 1 to 4 bytes. Second, confusing the digit 7, ASCII code 55, with the number 7. Third, forgetting the factor of 8 when converting bits to bytes, or the factor of 3 for 24-bit colour. Fourth, leaving out the number of channels when sizing stereo audio. Fifth, believing a bit map can be enlarged without loss; only vector graphics scale cleanly. Sixth, treating Unicode and UTF-8 as the same thing: one assigns numbers to characters, the other stores those numbers as bytes.

Key takeaways

Every kind of data needs an agreed encoding. ASCII uses 7 bits for 128 characters, usually stored in a byte. Unicode gives every character a code point, and UTF-8 stores it in 1 to 4 bytes, keeping ASCII unchanged. Binary stores numbers more compactly than character codes. Bit maps store pixels, commonly 24-bit RGB; vector graphics store shapes and scale without loss. Sound is sampled at a sample rate and bit depth: CD audio is 44,100 samples per second at 16 bits per channel. MIDI stores instructions, not sound.

Flashcards

Flashcard

Encoding

An agreement about which bit pattern stands for what

01000001 can be 65, A or a pixel value.

Test your understanding

Question 1 of 5

In ASCII, what is the 8-bit pattern for the digit character 5?

Question 2 of 5

How many bytes does the word Noël (N, o, ë, l) take in UTF-8?

Question 3 of 5

An uncompressed 800 × 600 image uses 24-bit RGB colour. How many bytes does it need?

Question 4 of 5

A mono recording uses 8,000 samples per second at 8 bits per sample. How many bytes does one minute need?

Question 5 of 5

Why does a logo stored as vector graphics stay sharp when it is enlarged, while a bit-map version turns blocky?

← Previous topicNext topic →