13.2 Encoding Text, Images, and Sound
Key Takeaways
- ASCII uses 7 bits for 128 characters ('A' = 65, 'a' = 97, '0' = 48); UTF-8 encodes Unicode in 1 to 4 bytes per character and matches ASCII for its first 128 characters.
- To decode with a given scheme, split the bits into the code lengths the scheme specifies and look up each code; for example, with A = 00, C = 01, G = 10, T = 11, the bits 01111000 decode to CTGA.
- A raster image stores a grid of pixels; uncompressed size in bytes is width × height × bits per pixel ÷ 8, so 800 × 600 at 24 bits is 1,440,000 bytes.
- 24-bit RGB color uses 8 bits (0–255) per channel, giving 16,777,216 colors, written in hexadecimal as #RRGGBB.
- Digital sound is sampled at regular intervals; the sampling rate must exceed twice the highest frequency, and uncompressed size is sample rate × bit depth × channels × seconds ÷ 8.
What this competency asks
Under "bits as the universal medium," ETS asks you to encode or decode data given the description of an encoding scheme. Questions may define an unfamiliar code and ask you to apply it, or they may assume standard encodings such as ASCII and RGB. The discussion questions also ask how numbers, characters, and colors are represented as bits.
Applying any encoding scheme
An encoding scheme is an agreed-upon mapping between information and bit patterns. To use one:
- Note whether codes are fixed-length (every code has the same number of bits) or variable-length.
- For fixed-length codes, split the bits into equal groups. For variable-length prefix codes, where no code is the start of another, read bits until they match a code, then start again.
- Look up each group.
Example (fixed length): DNA bases coded as A = 00, C = 01, G = 10, T = 11.
- Encode "GATC": 10 00 11 01, which gives
10001101. - Decode
01111000: 01 | 11 | 10 | 00, which is CTGA.
With 4 symbols, 2 bits each suffice, since 2² = 4. With 5 symbols you would need 3 bits (Section 13.1).
Example (variable length, prefix code): A = 0, B = 10, C = 11. Decoding 010110 reads 0 → A, 10 → B, 11 → C, 0 → A, giving ABCA. Section 13.3 shows how Huffman coding builds such codes to save space.
Text
| Encoding | Size per character | Coverage |
|---|---|---|
| ASCII | 7 bits (usually stored in 1 byte) | 128 characters: English letters, digits, punctuation, control codes |
| Extended 8-bit sets | 1 byte | 256 characters; different regions used conflicting tables |
| Unicode, UTF-8 | 1–4 bytes | Nearly all writing systems plus symbols and emoji; the first 128 code points match ASCII |
| Unicode, UTF-16 | 2 or 4 bytes | Used internally by Java and JavaScript |
Useful ASCII codes: space = 32, '0' = 48 through '9' = 57, 'A' = 65 through 'Z' = 90, 'a' = 97 through 'z' = 122. Lowercase letters are exactly 32 higher than uppercase, so 'a' − 'A' = 32. Because digit characters are codes, the character '7' (55) is not the number 7. Programs convert by subtracting '0'. Code comparisons also explain why "Zebra" sorts before "apple" (Section 7.2).
Text file size: characters × bytes per character. A 5,000-character essay in ASCII or plain-English UTF-8 is about 5,000 bytes.
Images
Raster (bitmap) images
A raster image is a grid of pixels. Two numbers determine its uncompressed size:
- Resolution: width × height in pixels.
- Color depth: bits per pixel. 1 bit gives black and white; 8 bits give 256 colors or grays; 24 bits give 16,777,216 colors.
Size in bytes = width × height × bits per pixel ÷ 8
For 800 × 600 at 24 bits: 480,000 pixels × 3 bytes = 1,440,000 bytes, about 1.44 MB. For 1920 × 1080 at 24 bits: 2,073,600 × 3 = 6,220,800 bytes, about 6.2 MB.
Doubling both the width and the height quadruples the pixel count and the file size.
RGB color
Screens mix red, green, and blue light. In 24-bit color each channel has 8 bits (0–255), written as hex pairs #RRGGBB:
| Hex | RGB | Color |
|---|---|---|
#FF0000 | 255, 0, 0 | Red |
#00FF00 | 0, 255, 0 | Green |
#0000FF | 0, 0, 255 | Blue |
#FFFF00 | 255, 255, 0 | Yellow (red + green light) |
#FFFFFF | 255, 255, 255 | White |
#000000 | 0, 0, 0 | Black |
#808080 | 128, 128, 128 | Medium gray |
A 32-bit color adds an 8-bit alpha channel for transparency.
Vector images
A vector image stores shapes as mathematical descriptions, such as lines, curves, fill colors, and coordinates (SVG, for example). It scales to any size without pixelation and is compact for logos, icons, and diagrams, but it is unsuitable for photographs. Raster images blur or pixelate when enlarged beyond their resolution.
Sound
Sound is a continuous wave. Analog-to-digital conversion measures (samples) its amplitude at regular intervals and stores each measurement as a number.
- Sampling rate: samples per second (Hz). The Nyquist–Shannon principle: to capture a frequency, sample at more than twice it. Human hearing reaches about 20 kHz, so CDs use 44.1 kHz.
- Bit depth: bits per sample. More bits give finer amplitude steps and a wider dynamic range: 16-bit (CD) has 65,536 levels, about 96 dB.
- Channels: 1 for mono, 2 for stereo.
Size in bytes = sample rate × bit depth × channels × seconds ÷ 8
For 4 minutes of CD-quality stereo: 44,100 × 16 × 2 × 240 ÷ 8 = 42,336,000 bytes, about 42 MB (roughly 10 MB per minute).
Sampling too slowly causes aliasing, where high frequencies masquerade as lower ones. Using fewer bits per sample adds audible noise.
The trade-off in every encoding
More bits per unit (per character, per pixel, per sample) means better fidelity and bigger files. Fewer bits means smaller files and less detail. Compression (Section 13.3) reduces size further, either exactly (lossless) or approximately (lossy).
A code represents the four DNA bases as A = 00, C = 01, G = 10, and T = 11. What does the bit string 01111000 decode to?
What is the uncompressed size of an 800 × 600-pixel image with 24-bit color?
An audio signal's highest frequency is 18 kHz. According to the Nyquist–Shannon principle, what sampling rate is needed to capture it without aliasing?
A web page uses the color #00FF00. What color does this code produce?