PROJECTENGRAM

[PS] UNICODE AND CHARACTER REPRESENTATION

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 77-bit ASCII historically sufficed for basic English alphanumeric characters, global computing necessitated the Unicode Worldwide Character Standard, defining over 1.11.1 million code points (U+000000\text{U+000000} through U+10FFFF\text{U+10FFFF}) partitioned across 1717 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 U+XXXX\text{U+XXXX} or U+XXXXXX\text{U+XXXXXX}.
  • Unicode Plane: A contiguous block of 65,53665{,}536 (2162^{16}) code points. Unicode encompasses 1717 planes (Plane 00 to Plane 1616), spanning U+0000\text{U+0000} to U+10FFFF\text{U+10FFFF} for a total capacity of 17×65,536=1,114,11217 \times 65{,}536 = 1{,}114{,}112 code points.
    • Plane 0 (BMP - Basic Multilingual Plane): U+0000\text{U+0000} to U+FFFF\text{U+FFFF}. Contains characters for virtually all modern written scripts and standard symbols.
    • Plane 1 (SMP - Supplementary Multilingual Plane): U+010000\text{U+010000} to U+01FFFF\text{U+01FFFF}. Historic scripts, musical notation, and emoji.
    • Plane 2 (SIP - Supplementary Ideographic Plane): U+020000\text{U+020000} to U+02FFFF\text{U+02FFFF}. Rare and historic CJK unified ideographs.
    • Plane 14 (SSP - Supplementary Special-purpose Plane): U+0E0000\text{U+0E0000} to U+0EFFFF\text{U+0EFFFF}. Tag characters and variation selectors.
    • Planes 15 & 16 (SPUA-A & SPUA-B - Supplementary Private Use Areas): U+0F0000\text{U+0F0000} to U+10FFFF\text{U+10FFFF}. 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 11, 22, 33, or 44 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 ≤U+FFFF\le \text{U+FFFF}) or two 16-bit units (Surrogate Pairs) for supplementary planes (U+10000\text{U+10000} to U+10FFFF\text{U+10FFFF}).
  • 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 [0xD800,0xDBFF][0\text{xD800}, 0\text{xDBFF}] and Low Surrogate in [0xDC00,0xDFFF][0\text{xDC00}, 0\text{xDFFF}]) used exclusively in UTF-16 to encode characters in supplementary planes.
  • Byte Order Mark (BOM): The Unicode character U+FEFF\text{U+FEFF} 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 RangeCode Point Bits (NN)Byte CountByte 1 (Prefix + Payload)Byte 2Byte 3Byte 4
U+0000…U+007F\text{U+0000} \dots \text{U+007F}ASCII subset77 bits110xxxxxxx———
U+0080…U+07FF\text{U+0080} \dots \text{U+07FF}Latin-1, Greek, Arabic1111 bits22110xxxxx10xxxxxx——
U+0800…U+FFFF\text{U+0800} \dots \text{U+FFFF}Rest of BMP (CJK, symbols)1616 bits331110xxxx10xxxxxx10xxxxxx—
U+10000…U+10FFFF\text{U+10000} \dots \text{U+10FFFF}Supplementary Planes2121 bits4411110xxx10xxxxxx10xxxxxx10xxxxxx

UTF-8 Properties:

  1. ASCII Backward Compatibility: Standard ASCII values (0x00…0x7F0\text{x00} \dots 0\text{x7F}) encode to identical single bytes (000000002…01111111200000000_2 \dots 01111111_2).
  2. 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.
  3. 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:

  1. Basic Multilingual Plane (U+0000≤U≤U+FFFF\text{U+0000} \le U \le \text{U+FFFF}):

    • Encoded directly as a single 16-bit word:
      UTF-16(U)=U\text{UTF-16}(U) = U
    • The code point sub-range U+D800…U+DFFF\text{U+D800} \dots \text{U+DFFF} is permanently reserved for surrogates and contains no assigned characters.
  2. Supplementary Planes (U+010000≤U≤U+10FFFF\text{U+010000} \le U \le \text{U+10FFFF}):

    • A code point in this range requires 2020 bits of information.
    • Step 1: Shift / Offset Calculation:
      W=U−0x10000(0≤W≤0xFFFFF)W = U - 0\text{x10000} \quad (0 \le W \le 0\text{xFFFFF})
      Here WW is a 20-bit binary integer: W=w19w18…w1w0W = w_{19} w_{18} \dots w_1 w_0.
    • Step 2: Partition into 10-bit Halves:
  • High 10 bits (WupperW_{\text{upper}}):
    Wupper=⌊W/210⌋=W≫10=w19..10(0≤Wupper≤0x3FF)W_{\text{upper}} = \lfloor W / 2^{10} \rfloor = W \gg 10 = w_{19..10} \quad (0 \le W_{\text{upper}} \le 0\text{x3FF})
  • Low 10 bits (WlowerW_{\text{lower}}):
    Wlower=W(mod210)=W & 0x3FF=w9..0(0≤Wlower≤0x3FF)W_{\text{lower}} = W \pmod{2^{10}} = W \ \& \ 0\text{x3FF} = w_{9..0} \quad (0 \le W_{\text{lower}} \le 0\text{x3FF})
    • Step 3: Add Surrogate Base Offsets:
  • High Surrogate (ShighS_{\text{high}}):
    Shigh=0xD800+Wupper(Range: 0xD800…0xDBFF)S_{\text{high}} = 0\text{xD800} + W_{\text{upper}} \quad (\text{Range: } 0\text{xD800} \dots 0\text{xDBFF})
  • Low Surrogate (SlowS_{\text{low}}):
    Slow=0xDC00+Wlower(Range: 0xDC00…0xDFFF)S_{\text{low}} = 0\text{xDC00} + W_{\text{lower}} \quad (\text{Range: } 0\text{xDC00} \dots 0\text{xDFFF})
    • The final encoded sequence is the pair of 16-bit words: [Shigh,Slow][S_{\text{high}}, S_{\text{low}}].

3. UTF-32 Encoding Scheme

UTF-32 represents every Unicode code point UU directly as a 32-bit unsigned integer (4 bytes):
UTF-32(U)=0x00 ∥ U23..16 ∥ U15..8 ∥ U7..0=0x00000000…0x0010FFFF\text{UTF-32}(U) = 0\text{x00} \,\|\, U_{23..16} \,\|\, U_{15..8} \,\|\, U_{7..0} = 0\text{x00000000} \dots 0\text{x0010FFFF}

While UTF-32 offers O(1)O(1) 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 FormatBOM Character (U+FEFF\text{U+FEFF}) Byte SequenceByte Order Interpretation
UTF-8EF BB BFOptional marker (signals UTF-8 encoding; no endianness).
UTF-16BEFE FFMost Significant Byte (MSB) stored first.
UTF-16LEFF FELeast Significant Byte (LSB) stored first.
UTF-32BE00 00 FE FFMSB stored at lowest address.
UTF-32LEFF FE 00 00LSB stored at lowest address.

◇Worked Examples and Step-by-Step Problem Solutions

Problem 1: Encoding Code Point U+10CAFE\text{U+10CAFE}

Problem Statement:
Given the Unicode code point U+10CAFE\text{U+10CAFE}, determine its binary and hexadecimal representations in:

  1. UTF-8
  2. UTF-16
  3. UTF-32

Show all intermediate steps, bit allocation templates, and final hex values.

Step 1: Analyze Code Point and Select Templates

  • Hexadecimal code point: U=0x10CAFEU = 0\text{x10CAFE}
  • Decimal value: 1,100,5421{,}100{,}542
  • Range check: U+10000≤U≤U+10FFFF\text{U+10000} \le U \le \text{U+10FFFF} (Plane 16 - Supplementary Private Use Area-B).
  • Binary expansion of 0x10CAFE0\text{x10CAFE} (2121 bits):
    0x10CAFE=0001 0000 1100 1010 1111 11102  ⟹  1 0000 1100 1010 1111 111020\text{x10CAFE} = 0001\ 0000\ 1100\ 1010\ 1111\ 1110_2 \implies 1\ 0000\ 1100\ 1010\ 1111\ 1110_2

Step 2: Derive UTF-8 Representation

For U∈[U+10000,U+10FFFF]U \in [\text{U+10000}, \text{U+10FFFF}], UTF-8 uses a 44-byte template:
Template: 11110xxx10xxxxxx10xxxxxx10xxxxxx\text{Template: } \mathbf{11110}xxx \quad \mathbf{10}xxxxxx \quad \mathbf{10}xxxxxx \quad \mathbf{10}xxxxxx

Partition the 21 payload bits from right to left:

  • Payload bits: 1 0000 1100 1010 1111 1110
  • Group 4 (LSB 6 bits): 111110 →\to Byte 4: 10 + 111110 =101111102=0xBE= 10111110_2 = 0\text{xBE}
  • Group 3 (Next 6 bits): 101011 →\to Byte 3: 10 + 101011 =101010112=0xAB= 10101011_2 = 0\text{xAB}
  • Group 2 (Next 6 bits): 001100 →\to Byte 2: 10 + 001100 =100011002=0x8C= 10001100_2 = 0\text{x8C}
  • Group 1 (MSB 3 bits): 100 →\to Byte 1: 11110 + 100 =111101002=0xF4= 11110100_2 = 0\text{xF4}
『』
TEXT
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 (or 0xF48CABBE)

Step 3: Derive UTF-16 Representation (Surrogate Pair Calculation)

Since U≥U+10000U \ge \text{U+10000}, UTF-16 requires a surrogate pair [Shigh,Slow][S_{\text{high}}, S_{\text{low}}]:

  1. Subtract base offset 0x100000\text{x10000}:
    W=0x10CAFE−0x10000=0x0FCAFEW = 0\text{x10CAFE} - 0\text{x10000} = 0\text{x0FCAFE}
    In binary (20 bits):
    W=0000 1111 1100 1010 1111 11102=1111 1100 1010 1111 11102W = 0000\ 1111\ 1100\ 1010\ 1111\ 1110_2 = 1111\ 1100\ 1010\ 1111\ 1110_2

  2. Split WW into upper and lower 10-bit words:

    • Upper 10 bits (W19..10W_{19..10}):
      W19..10=W≫10=⌊1034982/1024⌋=1010=0x03F2=11111100102W_{19..10} = W \gg 10 = \lfloor 1034982 / 1024 \rfloor = 1010 = 0\text{x03F2} = 1111110010_2
    • Lower 10 bits (W9..0W_{9..0}):
      W9..0=W & 0x3FF=1034982(mod1024)=766=0x02FE=10111111102W_{9..0} = W \ \& \ 0\text{x3FF} = 1034982 \pmod{1024} = 766 = 0\text{x02FE} = 1011111110_2
  3. Compute Surrogates:

    • High Surrogate (ShighS_{\text{high}}):
      Shigh=0xD800+W19..10=0xD800+0x03F2=0xDBF2S_{\text{high}} = 0\text{xD800} + W_{19..10} = 0\text{xD800} + 0\text{x03F2} = 0\text{xDBF2}
      Binary: 1101 1011 1111 00102\text{Binary: } 1101\ 1011\ 1111\ 0010_2
    • Low Surrogate (SlowS_{\text{low}}):
      Slow=0xDC00+W9..0=0xDC00+0x02FE=0xDEFES_{\text{low}} = 0\text{xDC00} + W_{9..0} = 0\text{xDC00} + 0\text{x02FE} = 0\text{xDEFE}
      Binary: 1101 1110 1111 11102\text{Binary: } 1101\ 1110\ 1111\ 1110_2
  • 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=0x0010CAFE\text{UTF-32} = 0\text{x0010CAFE}

  • 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 U+FCAFE\text{U+FCAFE}

Problem Statement:
Given the Unicode code point U+FCAFE\text{U+FCAFE}, determine its binary and hexadecimal representations in:

  1. UTF-8
  2. UTF-16
  3. UTF-32

Show all intermediate steps, bit allocation templates, and final hex values.

Step 1: Analyze Code Point and Select Templates

  • Hexadecimal code point: U=0xFCAFEU = 0\text{xFCAFE}
  • Decimal value: 1,035,0061{,}035{,}006
  • Range check: U+10000≤U≤U+10FFFF\text{U+10000} \le U \le \text{U+10FFFF} (Plane 15 - Supplementary Private Use Area-A).
  • Binary expansion of 0xFCAFE0\text{xFCAFE} (2020 bits, padded to 2121 bits):
    0xFCAFE=0000 1111 1100 1010 1111 11102  ⟹  0 1111 1100 1010 1111 111020\text{xFCAFE} = 0000\ 1111\ 1100\ 1010\ 1111\ 1110_2 \implies 0\ 1111\ 1100\ 1010\ 1111\ 1110_2

Step 2: Derive UTF-8 Representation

4-byte UTF-8 template:
Template: 11110xxx10xxxxxx10xxxxxx10xxxxxx\text{Template: } \mathbf{11110}xxx \quad \mathbf{10}xxxxxx \quad \mathbf{10}xxxxxx \quad \mathbf{10}xxxxxx

Partition the 21 payload bits (0 1111 1100 1010 1111 1110):

  • Group 4 (LSB 6 bits): 111110 →\to Byte 4: 10 + 111110 =101111102=0xBE= 10111110_2 = 0\text{xBE}
  • Group 3 (Next 6 bits): 101011 →\to Byte 3: 10 + 101011 =101010112=0xAB= 10101011_2 = 0\text{xAB}
  • Group 2 (Next 6 bits): 111100 →\to Byte 2: 10 + 111100 =101111002=0xBC= 10111100_2 = 0\text{xBC}
  • Group 1 (MSB 3 bits): 011 →\to Byte 1: 11110 + 011 =111100112=0xF3= 11110011_2 = 0\text{xF3}
『』
TEXT
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 (or 0xF3BCABBE)

Step 3: Derive UTF-16 Representation (Surrogate Pair Calculation)

Since U≥U+10000U \ge \text{U+10000}, compute the surrogate pair:

  1. Subtract base offset 0x100000\text{x10000}:
    W=0xFCAFE−0x10000=0xECAFEW = 0\text{xFCAFE} - 0\text{x10000} = 0\text{xECAFE}
    In binary (20 bits):
    W=1110 1100 1010 1111 11102W = 1110\ 1100\ 1010\ 1111\ 1110_2

  2. Split WW into upper and lower 10-bit words:

    • Upper 10 bits (W19..10W_{19..10}):
      W19..10=W≫10=⌊969470/1024⌋=946=0x03B2=11101100102W_{19..10} = W \gg 10 = \lfloor 969470 / 1024 \rfloor = 946 = 0\text{x03B2} = 1110110010_2
    • Lower 10 bits (W9..0W_{9..0}):
      W9..0=W & 0x3FF=969470(mod1024)=766=0x02FE=10111111102W_{9..0} = W \ \& \ 0\text{x3FF} = 969470 \pmod{1024} = 766 = 0\text{x02FE} = 1011111110_2
  3. Compute Surrogates:

    • High Surrogate (ShighS_{\text{high}}):
      Shigh=0xD800+W19..10=0xD800+0x03B2=0xDBB2S_{\text{high}} = 0\text{xD800} + W_{19..10} = 0\text{xD800} + 0\text{x03B2} = 0\text{xDBB2}
      Binary: 1101 1011 1011 00102\text{Binary: } 1101\ 1011\ 1011\ 0010_2
    • Low Surrogate (SlowS_{\text{low}}):
      Slow=0xDC00+W9..0=0xDC00+0x02FE=0xDEFES_{\text{low}} = 0\text{xDC00} + W_{9..0} = 0\text{xDC00} + 0\text{x02FE} = 0\text{xDEFE}
      Binary: 1101 1110 1111 11102\text{Binary: } 1101\ 1110\ 1111\ 1110_2
  • 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=0x000FCAFE\text{UTF-32} = 0\text{x000FCAFE}

  • 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 PointUnicode Name / CategoryUTF-8 (Hex)UTF-16BE (Hex)UTF-32BE (Hex)Total Storage (Bytes)
U+0041Basic Latin 'A'4100 4100 00 00 41UTF-8: 1B, UTF-16: 2B, UTF-32: 4B
U+03A3Greek Capital Sigma 'Σ\Sigma'CE A303 A300 00 03 A3UTF-8: 2B, UTF-16: 2B, UTF-32: 4B
U+20ACCurrency Euro '€'E2 82 AC20 AC00 00 20 ACUTF-8: 3B, UTF-16: 2B, UTF-32: 4B
U+245D6CJK Extension B IdeographF0 A4 97 96D8 51 DD D600 02 45 D6UTF-8: 4B, UTF-16: 4B, UTF-32: 4B
U+FCAFESupplementary Private Use Area-AF3 BC AB BEDB B2 DE FE00 0F CA FEUTF-8: 4B, UTF-16: 4B, UTF-32: 4B
U+10CAFESupplementary Private Use Area-BF4 8C AB BEDB F2 DE FE00 10 CA FEUTF-8: 4B, UTF-16: 4B, UTF-32: 4B

◇Common Pitfalls & Edge Cases

[!warning] Critical Encoding Traps

  1. Overlong Encodings in UTF-8: A security vulnerability where a character is encoded using more bytes than necessary (e.g., encoding ASCII 'A' (0x410\text{x41}) as 11000001 10000001_2 (0xC1 0x810\text{xC1} \, 0\text{x81}) instead of 01000001_2 (0x410\text{x41})). Decoders must reject overlong sequences to prevent path traversal or filtering bypass attacks.
  2. Illegal Surrogate Code Points in UTF-8/UTF-32: The surrogate range U+D800…U+DFFF\text{U+D800} \dots \text{U+DFFF} 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.
  3. Endianness Confusion in UTF-16: UTF-16 code units require endianness specification when transmitted over byte streams. 0xDBF2 0xDEFE in UTF-16BE becomes F2 DB FE DE in UTF-16LE. Missing BOM markers can lead to inverted character interpretation.
  4. Surrogate Math Off-by-One: Forgetting to subtract 0x100000\text{x10000} before splitting into 10-bit chunks yields an invalid surrogate range exceeding 0xDBFF0\text{xDBFF}.

◇References & Navigation