{
  "id": 5893218,
  "title": "Why JavaScript Text-to-Binary Snippets Break on Emoji",
  "url": "https://urgent.news/2026/09/06/why-javascript-text-to-binary-snippets-break-on-emoji",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-09-06T02:41:00.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/joseph_laurence-best-lucky2026/why-javascript-text-to-binary-snippets-break-on-emoji-3a5e"
  },
  "original_language": "en",
  "account": "JavaScript text-to-binary snippets often fail when converting emoji, as the function may return incorrect bytes. The issue stems from how JavaScript strings are handled, where strings are sequences of UTF-16 code units. The emoji's Unicode code point is represented by a surrogate pair: 0xD83D and 0xDE42. When using split( ), this surrogate pair is divided, leading to inaccuracies.\n\nThe function naiveBinary splits the text into characters, converts each to its charCodeAt value, converts that to a binary string padded to eight bits, and then joins the bytes. However, this method doesn't accurately represent the UTF-8 encoding. For example, the emoji 🙂 produces two 16-bit groups, neither of which is a valid UTF-8 byte.\n\nTo correctly convert text to binary, start by encoding the text using TextEncoder.encode(), which returns a Uint8Array containing UTF-8 bytes. Then, format each byte by converting it to a binary string padded to eight bits. This ensures that each value is a valid byte between 0 and 255. Displaying spaces between bytes aids in visualizing byte boundaries but does not affect the encoded text.\n\nConverting binary back to text requires validation. First, split the binary string into groups of exactly eight bits. Each group must be a valid binary number. Then, convert each group from binary to a decimal number and create a Uint8Array from these numbers. Finally, decode the bytes using TextDecoder.decode() with the 'utf-8' encoding. This process ensures that both the encoding and decoding handle the text correctly, rejecting any malformed input.",
  "summary": "A text-to-binary function can pass every test with Hello and still produce the wrong bytes for 🙂 . The catch is deciding what “binary” means. For this article, the target is UTF-8 bytes, displayed as eight-bit groups . JavaScript string operations don't automatically give you those bytes. Consider this implementation: function naiveBinary ( text ) { return text . split ( \"\" ) . map ( char =>…",
  "key_points": [],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}