Why UTF-8 Byte Encoding Differs From JavaScript charCodeAt()
Every ASCII character (U+0000 to U+007F) maps identically to a single 8-bit UTF-8 byte (padded with leading zeros to 8 bits, e.g., 'A' = 65 = 01000001). Beyond ASCII, JavaScript strings store UTF-16 code units internally, so naive charCodeAt().toString(2) loops emit 16-bit UTF-16 surrogate pairs rather than the UTF-8 bytes sent over HTTP, TCP, and disk files.
Encoding with TextEncoder converts strings into canonical RFC 3629 UTF-8 octets: 2 bytes for accented Latin/Greek/Cyrillic, 3 bytes for CJK and currency symbols like €, and 4 bytes for emoji. That matches the exact binary payload stored by Go, Rust, Python, and PostgreSQL.
- Encoding EngineUses Web TextEncoder (UTF-8) so every output group is a true 8-bit wire octet.
- Length TelemetryCompares Unicode code points, JS UTF-16 .length, and exact UTF-8 byte count.
- Bit DelimitersSupports space-separated octets, 0b-prefixed tokens, or contiguous bitstreams.
Why does a single emoji produce four 8-bit binary groups?
Emojis live in Unicode supplementary planes (above U+FFFF) and require 4 bytes in UTF-8 encoding (starting with 11110xxx followed by three 10xxxxxx continuation bytes).
Should binary bytes always be padded to 8 bits?
Yes. Without leading zero padding (e.g., 01000001 instead of 1000001), concatenating bytes makes byte boundaries ambiguous during decoding.
How many bits are in a 10-character ASCII string?
Each ASCII character occupies 1 byte (8 bits), so a 10-character ASCII string equals 10 bytes or 80 bits.