UNICODE TEXT

Finding a truncated UTF-8 character in hexadecimal bytes

A hexadecimal string can be made of perfectly good byte pairs and still stop in the middle of a character. This guide shows how to tell a complete UTF-8 sequence from a truncated one.

Two checks that fail for different reasons

A decoder for hexadecimal UTF-8 input asks two separate questions. The first is about notation: does every token have exactly two hexadecimal digits, so that C3 and A9 are bytes while C and A9F are not? The second is about encoding: do those bytes, read in order, follow the UTF-8 rules for one or more Unicode characters? An input can pass the first check and fail the second. That is the situation with a lone C3. The token is a legitimate byte, yet the sequence it forms is incomplete. Keeping the two layers apart tells you where to look for the fault. A notation error points to typing or copying. An encoding error points to how the bytes were produced or cut.

What the first byte promises

UTF-8 announces its own length in the first byte of every character. A byte whose top bit is 0 is a complete one-byte character. A byte that starts with the bits 110 promises one more byte, 1110 promises two more, and 11110 promises three more. Every following byte must start with the bits 10. C3 is 11000011 in binary, so it begins with 110 and commits the stream to exactly one continuation byte. When the input ends right after C3, the decoder is still waiting for that second byte. RFC 3629 defines these rules and limits UTF-8 to code points up to U+10FFFF, which means no character needs more than four bytes.

Why C3 A9 decodes to an accented letter

The complete pair C3 A9 shows the promise being kept. The two bytes carry the payload bits 00011 from C3 and 101001 from A9. Joined, they read 00011101001, which is hexadecimal E9 and Unicode code point U+00E9, the lowercase letter e with an acute accent. The same character in a Latin-1 file is the single byte E9, which is why moving text between encodings so often produces trouble. In UTF-8, E9 alone is a lead byte that promises two continuation bytes, so a Latin-1 byte stream fed to a strict UTF-8 decoder is rejected instead of read as an accented letter. Seeing C3 A9 decode correctly while C3 alone is refused is the quickest way to confirm that the tool is checking the whole sequence.

Three-byte and four-byte characters truncate in more ways

Longer characters can be cut at more than one point. The euro sign is E2 82 AC. Stopping after E2 leaves two missing bytes, and stopping after E2 82 leaves one. Both prefixes are invalid even though each byte looks harmless. An emoji such as the grinning face, U+1F600, is F0 9F 98 80, so there are three possible truncation points before the final byte arrives. A strict decoder rejects every one of them. The decoded character appears only when the full group is present. This matters for text in languages that use three-byte characters, including Chinese, Japanese and Korean, because a cut that happens to land inside a character is common there, and a cut that lands exactly on a boundary is the exception.

A single byte that needs no partner

The third control in the worked cases uses 41. Its top bit is 0, so it is a complete one-byte character, the capital letter A, and nothing is expected after it. All of ASCII works this way: bytes 00 through 7F decode on their own and mean the same in ASCII and in UTF-8. This is the reason the length of a hexadecimal input does not tell you how many characters it holds. Six bytes can be six ASCII letters, three two-byte letters, two three-byte characters, or a mixture. Only the lead bytes reveal the grouping, and a prefix that ends before the promised continuation bytes arrive can never decode to a character.

Where truncation usually comes from

Truncated sequences rarely come from typing. They appear when a system cuts text by bytes rather than by characters. A database column limited to 255 bytes, a network buffer that splits a message, a log line clipped at a fixed width, or a command that reads the first N bytes of a file can all land inside a multibyte character. Copying a hexadecimal dump from a debugger and missing the last pair has the same effect. When a decoder rejects an input, compare the byte count with the length the producer claimed. A difference of one or two bytes at the end is a strong sign that the cut happened after the data was encoded.

Reading lead bytes by their range

You can classify a byte by eye. Values from 00 to 7F are complete single characters. Values from 80 to BF can only be continuation bytes and can never begin a character, so a sequence that starts with one of them is already broken. C2 to DF begin two-byte characters, E0 to EF begin three-byte characters, and F0 to F4 begin four-byte characters. The values C0, C1 and F5 to FF never appear in valid UTF-8 at all. Scanning a hexadecimal dump with these ranges in mind lets you spot a cut in seconds: find the last lead byte, count how many bytes it promised, and compare that with how many bytes actually follow it.

Why the tool refuses instead of substituting

Many programs handle bad UTF-8 by replacing it with U+FFFD, the replacement character, whose own encoding is EF BF BD. That keeps text flowing but hides the damage, and the original bytes cannot be reconstructed from the replacement. This tool rejects malformed input with a message that the bytes are not valid UTF-8, so the faulty sequence stays visible. Strictness also matches how security-sensitive software treats input. Overlong forms, such as writing a slash as C0 AF, and encoded surrogate halves in the range U+D800 to U+DFFF are invalid in UTF-8 and must not be accepted silently. A decoder that rejects instead of repairing is giving you the true state of the data.

Recovering the original text

Do not append a guessed byte to make a rejected input decode. A lead byte of C3 can be followed by any of 64 continuation values, and each one produces a different character, so a guess changes the meaning of the text without any warning. Go back to the source instead. Re-export the data, read the remaining bytes from the file or packet, or find the cut and correct the limit that caused it. If the whole input is a legacy encoding, decode it with the right character set first. Keep the original bytes alongside the rejected result so the next person can see exactly where the stream ended, and truncate in future by character boundaries, not by raw byte positions.

Reproduce the checks

Complete accented letter

Open the Utf8 Byte Converter and enter:

Calculation mode
decode
Text or encoded input
C3 A9

Expected output:

Hex

é

Both bytes of the encoded character are present.

Truncated prefix

Open the Utf8 Byte Converter and enter:

Calculation mode
decode
Text or encoded input
C3

Expected rejection:

not valid UTF-8

The hexadecimal token is valid but the text sequence is incomplete.

An ASCII byte needs no continuation

Open the Utf8 Byte Converter and enter:

Calculation mode
decode
Text or encoded input
41

Expected output:

Hex

A

Byte-sequence length depends on the encoded character.

References and implementation scope

The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.

Sources

Related tasks