Diagnose a Character Encoding Bug (Mojibake) by Finding the Exact Hop That Mis-Decodes the Bytes
Help me diagnose a character encoding bug, the kind where users see garbled text such as "café" instead of "café", "’" instead of an apostrophe, question marks, or replacement characters (�). Work from evidence toward the exact point in the data path where bytes are being decoded or encoded with the wrong charset. Do not suggest blanket fixes like "use UTF-8 everywhere" until we have located the faulty step. Inputs I will provide: - One or more examples of the garbled text exactly as it appears, and what it should have said - If possible, the raw bytes in hex for a bad example (e.g. from xxd or a hex dump), from as many stages as I can capture - The data path: where the text originates (user input, file upload, third-party API, database, CSV import) and every hop it passes through until it is displayed - Relevant configuration: database/table/column charset and collation, connection charset settings, HTTP Content-Type headers, file-reading code, and any language-level defaults Steps: 1. From the garbled sample alone, infer the most likely mis-decoding (for example, UTF-8 bytes read as Windows-1252 or Latin-1, double-encoded UTF-8, or a lossy conversion to a single-byte charset). Show the byte-level reasoning for one character. 2. Say whether the damage looks reversible (bytes intact but mislabeled) or lossy (bytes replaced with ? or U+FFFD), since that decides whether existing data can be repaired. 3. Walk the data path hop by hop and list, for each hop, what charset it assumes, how that is configured, and whether that is confirmed or assumed. 4. Name the hop most likely to be at fault, and propose the single cheapest check that would confirm it (e.g. dump bytes right before and after that hop). 5. Only then propose the fix for the faulty hop, plus a separate, cautious plan for repairing already-stored data if it is reversible, including how to identify affected rows without touching correct ones. Flag anything I have not given you that would change the diagnosis, and ask for it instead of assuming.
Sign in to upvote this prompt.