Problem Set - Unicode and Character Representation
Tags: #◇Computer Science #◇CSARCH2 #◇Computer Architecture #◇Data Representation #◇Unicode #◇Character Encoding
◇Overview
Character data representation is a foundational pillar of computer systems, bridging human-readable textual symbols with low-level binary architectures. While standard -bit ASCII historically sufficed for basic English alphanumeric characters, global computing necessitated the Unicode Worldwide Character Standard, defining over million code points ( through ) partitioned across discrete planes. This problem set investigates the mathematical mechanics and bit-level transformations required to encode Unicode code points into variable-length UTF-8 (1 to 4 octets), variable-length UTF-16 (using single 16-bit wydes or surrogate pairs with high and low surrogate offsets), and fixed-width UTF-32, alongside memory endianness considerations and byte order marks (BOM).
◇Key Definitions
- Character Repertoire: A collection of distinct abstract symbols and typographic glyphs (e.g., Latin letters, Greek alphabets, CJK ideographs, mathematical symbols, and emoji).
- Code Point: An integer value assigned to a specific abstract character within the Unicode codespace, conventionally denoted in hexadecimal as or .
- Unicode Plane: A contiguous block of () code points. Unicode encompasses planes (Plane to Plane ), spanning to for a total capacity of code points.
- Plane 0 (BMP - Basic Multilingual Plane): to . Contains characters for virtually all modern written scripts and standard symbols.
- Plane 1 (SMP - Supplementary Multilingual Plane): to . Historic scripts, musical notation, and emoji.
- Plane 2 (SIP - Supplementary Ideographic Plane): to . Rare and historic CJK unified ideographs.
- Plane 14 (SSP - Supplementary Special-purpose Plane): to . Tag characters and variation selectors.
- Planes 15 & 16 (SPUA-A & SPUA-B - Supplementary Private Use Areas): to . User/vendor-defined private characters.
- Code Unit: The fundamental minimal bit combination used by a specific encoding scheme to represent encoded text (e.g., 8-bit octet for UTF-8, 16-bit wyde for UTF-16, 32-bit dword for UTF-32).
- UTF-8 (Unicode Transformation Format - 8-bit): A variable-width encoding format mapping code points to , , , or bytes. It is backward-compatible with 7-bit ASCII and self-synchronizing.
- UTF-16 (Unicode Transformation Format - 16-bit): A variable-width encoding format mapping code points to either one 16-bit unit (for BMP characters ) or two 16-bit units (Surrogate Pairs) for supplementary planes ( to ).
- UTF-32 (Unicode Transformation Format - 32-bit): A fixed-width 32-bit encoding where every code point is directly zero-extended to an unsigned 32-bit integer.
- Surrogate Pair: A pair of 16-bit code units (High Surrogate in and Low Surrogate in ) used exclusively in UTF-16 to encode characters in supplementary planes.
- Byte Order Mark (BOM): The Unicode character placed at the beginning of a data stream to signal endianness (Big-Endian vs Little-Endian).
◇Theoretical Framework & Encoding Mechanics
1. UTF-8 Encoding Scheme
UTF-8 partitions the code space into four distinct bit-length ranges. Prefix bits in the first octet indicate the total byte length, while continuation octets strictly start with prefix bits 10:
| Code Point Range (Hex) | Character Range | Code Point Bits () | Byte Count | Byte 1 (Prefix + Payload) | Byte 2 | Byte 3 | Byte 4 |
|---|---|---|---|---|---|---|---|
| ASCII subset | bits | 0xxxxxxx | — | — | — | ||
| Latin-1, Greek, Arabic | bits | 110xxxxx | 10xxxxxx | — | — | ||
| Rest of BMP (CJK, symbols) | bits | 1110xxxx | 10xxxxxx | 10xxxxxx | — | ||
| Supplementary Planes | bits | 11110xxx | 10xxxxxx | 10xxxxxx | 10xxxxxx |
UTF-8 Properties:
- ASCII Backward Compatibility: Standard ASCII values () encode to identical single bytes ().
- Self-Synchronization & Error Resilience: If bytes are corrupted or lost during transmission, a decoder can immediately locate the start of the next character by searching for an octet not starting with
10xxxxxx. - No Endianness Ambiguity: Because the code unit is an 8-bit octet, UTF-8 is byte-order agnostic and does not require endian serialization flags.
2. UTF-16 Encoding Scheme & Surrogate Calculations
UTF-16 processes code points according to their plane:
-
Basic Multilingual Plane ():
- Encoded directly as a single 16-bit word:
- The code point sub-range is permanently reserved for surrogates and contains no assigned characters.
- Encoded directly as a single 16-bit word:
-
Supplementary Planes ():
- A code point in this range requires bits of information.
- Step 1: Shift / Offset Calculation:
Here is a 20-bit binary integer: . - Step 2: Partition into 10-bit Halves:
- High 10 bits ():
- Low 10 bits ():
- Step 3: Add Surrogate Base Offsets:
- High Surrogate ():
- Low Surrogate ():
- The final encoded sequence is the pair of 16-bit words: .
3. UTF-32 Encoding Scheme
UTF-32 represents every Unicode code point directly as a 32-bit unsigned integer (4 bytes):
While UTF-32 offers random indexing for characters, it incurs significant memory overhead (up to 4× memory waste for ASCII-heavy text).
4. Endianness and Byte Order Mark (BOM)
Because 16-bit and 32-bit code units span multiple bytes in physical memory, byte serialization depends on hardware architecture:
| Encoding Format | BOM Character () Byte Sequence | Byte Order Interpretation |
|---|---|---|
| UTF-8 | EF BB BF | Optional marker (signals UTF-8 encoding; no endianness). |
| UTF-16BE | FE FF | Most Significant Byte (MSB) stored first. |
| UTF-16LE | FF FE | Least Significant Byte (LSB) stored first. |
| UTF-32BE | 00 00 FE FF | MSB stored at lowest address. |
| UTF-32LE | FF FE 00 00 | LSB stored at lowest address. |
◇Worked Examples and Step-by-Step Problem Solutions
Problem 1: Encoding Code Point
Problem Statement:
Given the Unicode code point , determine its binary and hexadecimal representations in:
- UTF-8
- UTF-16
- UTF-32
Show all intermediate steps, bit allocation templates, and final hex values.
Step 1: Analyze Code Point and Select Templates
- Hexadecimal code point:
- Decimal value:
- Range check: (Plane 16 - Supplementary Private Use Area-B).
- Binary expansion of ( bits):
Step 2: Derive UTF-8 Representation
For , UTF-8 uses a -byte template:
Partition the 21 payload bits from right to left:
- Payload bits:
1 0000 1100 1010 1111 1110 - Group 4 (LSB 6 bits):
111110Byte 4:10+111110 - Group 3 (Next 6 bits):
101011Byte 3:10+101011 - Group 2 (Next 6 bits):
001100Byte 2:10+001100 - Group 1 (MSB 3 bits):
100Byte 1:11110+100
Byte 1: [11110 100] = 11110100_2 = 0xF4
Byte 2: [10 001100] = 10001100_2 = 0x8C
Byte 3: [10 101011] = 10101011_2 = 0xAB
Byte 4: [10 111110] = 10111110_2 = 0xBE
- UTF-8 Binary:
11110100 10001100 10101011 10111110 - UTF-8 Hex:
F4 8C AB BE(or0xF48CABBE)
Step 3: Derive UTF-16 Representation (Surrogate Pair Calculation)
Since , UTF-16 requires a surrogate pair :
-
Subtract base offset :
In binary (20 bits):
-
Split into upper and lower 10-bit words:
- Upper 10 bits ():
- Lower 10 bits ():
- Upper 10 bits ():
-
Compute Surrogates:
- High Surrogate ():
- Low Surrogate ():
- High Surrogate ():
- UTF-16 Code Units:
0xDBF2 0xDEFE - UTF-16 Hex (Big-Endian):
DB F2 DE FE - UTF-16 Hex (Little-Endian):
F2 DB FE DE
Step 4: Derive UTF-32 Representation
Directly zero-extend the 21-bit code point to 32 bits:
- UTF-32 Binary:
00000000 00010000 11001010 11111110 - UTF-32 Hex (Big-Endian):
00 10 CA FE - UTF-32 Hex (Little-Endian):
FE CA 10 00
Problem 2: Encoding Code Point
Problem Statement:
Given the Unicode code point , determine its binary and hexadecimal representations in:
- UTF-8
- UTF-16
- UTF-32
Show all intermediate steps, bit allocation templates, and final hex values.
Step 1: Analyze Code Point and Select Templates
- Hexadecimal code point:
- Decimal value:
- Range check: (Plane 15 - Supplementary Private Use Area-A).
- Binary expansion of ( bits, padded to bits):
Step 2: Derive UTF-8 Representation
4-byte UTF-8 template:
Partition the 21 payload bits (0 1111 1100 1010 1111 1110):
- Group 4 (LSB 6 bits):
111110Byte 4:10+111110 - Group 3 (Next 6 bits):
101011Byte 3:10+101011 - Group 2 (Next 6 bits):
111100Byte 2:10+111100 - Group 1 (MSB 3 bits):
011Byte 1:11110+011
Byte 1: [11110 011] = 11110011_2 = 0xF3
Byte 2: [10 111100] = 10111100_2 = 0xBC
Byte 3: [10 101011] = 10101011_2 = 0xAB
Byte 4: [10 111110] = 10111110_2 = 0xBE
- UTF-8 Binary:
11110011 10111100 10101011 10111110 - UTF-8 Hex:
F3 BC AB BE(or0xF3BCABBE)
Step 3: Derive UTF-16 Representation (Surrogate Pair Calculation)
Since , compute the surrogate pair:
-
Subtract base offset :
In binary (20 bits):
-
Split into upper and lower 10-bit words:
- Upper 10 bits ():
- Lower 10 bits ():
- Upper 10 bits ():
-
Compute Surrogates:
- High Surrogate ():
- Low Surrogate ():
- High Surrogate ():
- UTF-16 Code Units:
0xDBB2 0xDEFE - UTF-16 Hex (Big-Endian):
DB B2 DE FE - UTF-16 Hex (Little-Endian):
B2 DB FE DE
Step 4: Derive UTF-32 Representation
Directly zero-extend to 32 bits:
- UTF-32 Binary:
00000000 00001111 11001010 11111110 - UTF-32 Hex (Big-Endian):
00 0F CA FE - UTF-32 Hex (Little-Endian):
FE CA 0F 00
Comparative Summary Table
| Character Code Point | Unicode Name / Category | UTF-8 (Hex) | UTF-16BE (Hex) | UTF-32BE (Hex) | Total Storage (Bytes) |
|---|---|---|---|---|---|
U+0041 | Basic Latin 'A' | 41 | 00 41 | 00 00 00 41 | UTF-8: 1B, UTF-16: 2B, UTF-32: 4B |
U+03A3 | Greek Capital Sigma '' | CE A3 | 03 A3 | 00 00 03 A3 | UTF-8: 2B, UTF-16: 2B, UTF-32: 4B |
U+20AC | Currency Euro '€' | E2 82 AC | 20 AC | 00 00 20 AC | UTF-8: 3B, UTF-16: 2B, UTF-32: 4B |
U+245D6 | CJK Extension B Ideograph | F0 A4 97 96 | D8 51 DD D6 | 00 02 45 D6 | UTF-8: 4B, UTF-16: 4B, UTF-32: 4B |
U+FCAFE | Supplementary Private Use Area-A | F3 BC AB BE | DB B2 DE FE | 00 0F CA FE | UTF-8: 4B, UTF-16: 4B, UTF-32: 4B |
U+10CAFE | Supplementary Private Use Area-B | F4 8C AB BE | DB F2 DE FE | 00 10 CA FE | UTF-8: 4B, UTF-16: 4B, UTF-32: 4B |
◇Common Pitfalls & Edge Cases
[!warning] Critical Encoding Traps
- Overlong Encodings in UTF-8: A security vulnerability where a character is encoded using more bytes than necessary (e.g., encoding ASCII
'A'() as11000001 10000001_2() instead of01000001_2()). Decoders must reject overlong sequences to prevent path traversal or filtering bypass attacks.- Illegal Surrogate Code Points in UTF-8/UTF-32: The surrogate range is designated strictly for UTF-16 surrogate pairs. Direct encoding of individual surrogate halves into UTF-8 or UTF-32 is strictly prohibited by RFC 3629 / Unicode Standard.
- Endianness Confusion in UTF-16: UTF-16 code units require endianness specification when transmitted over byte streams.
0xDBF2 0xDEFEin UTF-16BE becomesF2 DB FE DEin UTF-16LE. Missing BOM markers can lead to inverted character interpretation.- Surrogate Math Off-by-One: Forgetting to subtract before splitting into 10-bit chunks yields an invalid surrogate range exceeding .
◇References & Navigation
- Related Notes: ◇Integer Representation & Fixed-Point Arithmetic