A sensor produces a 40-character text message containing 20 occurrences of A, 10 of B, 6 of C and 4 of D. Its original representation uses one byte per character and has no header.
A compressor uses the following prefix code, in which no character's complete bit pattern is the beginning of another character's bit pattern.
Character
Compressed bit pattern
A
0
B
10
C
110
D
111
The compressed file contains a fixed 320-bit header followed by the payload. The header includes the decoding table and the meaningful payload length. The complete file is padded with zero bits to the next whole byte only when necessary. One byte contains eight bits, and there is no other overhead.
Find the payload length for one message in bits and the complete compressed file size for one message in bytes. Then find the smallest positive integer number of identical message copies that makes a single compressed file strictly smaller than the corresponding original text. When copies share a file, there is one header and padding only at the end; the compressor applies the same code separately to every character without any additional compression.
Give all three answers as exact integers.
Unlock full course notes by signing up
Diagnosing garbled accented text
A recipe website saves the word jalapeño in a text file encoded as UTF-8. A spreadsheet program opens the file assuming the single-byte Windows-1252 encoding and displays the word as jalapeño, while every other letter appears correctly. Which explanation accounts for this display?
Storage size of a message in three Unicode encodings
A chat message contains 14 characters: 8 ASCII characters, 3 Cyrillic letters with code points between U+0410 and U+044F, 2 Chinese characters with code points between U+4E00 and U+9FFF, and 1 emoji with code point U+1F600. Ignoring any byte-order mark, calculate how many bytes the message occupies when encoded in UTF-8, in UTF-16 and in UTF-32. Give each answer as a whole number of bytes.
When run-length encoding saves or costs bytes
A text compressor uses run-length encoding: it replaces consecutive copies of the same character with records containing a character and its repetition count. Each record occupies exactly two bytes: one byte for the character and one byte for a count from 1 through 255.
The compressor uses the longest possible record at each position. More than 255 consecutive copies require additional records. The original text uses one byte per character, and neither format has a header or other overhead.
Complete the table with exact integers. Calculate signed byte savings as original bytes minus compressed bytes; a negative saving means the compressed version is larger.
Decoding a four-byte UTF-8 sequence
A network packet contains the four bytes F0 9F 8E B5, given in hexadecimal, which together encode a single character in UTF-8. Decode the sequence and give the character's Unicode code point in the form U+ followed by hexadecimal digits, without leading zeros.