Checking malformed UTF-8 beyond a truncated sequence
A UTF-8 byte list can have the right number of bytes for a character and still be invalid. This guide compares a valid two-byte sequence with an overlong form and a surrogate encoding, both of which the strict decoder rejects.
What makes a UTF-8 sequence valid
UTF-8, specified in RFC 3629 and in the Unicode Standard, encodes each scalar value in one to four bytes. A lead byte announces the length: 0xxxxxxx for one byte, 110xxxxx for two, 1110xxxx for three and 11110xxx for four. Each continuation byte has the form 10xxxxxx. Length alone does not make a sequence valid. The decoded value must be a scalar value, so no surrogates and nothing above U+10FFFF, and the sequence must be the shortest form that can represent it. Validity is a property of the whole byte list, which is why a decoder that checks only the bit patterns can accept data that a strict decoder correctly refuses.
The valid control: C3 A9
The control case decodes the bytes C3 A9. C3 is 11000011, a two-byte lead carrying the bits 00011. A9 is 10101001, a continuation carrying 101001. Joining the payload bits gives 00011101001, which is 0xE9, the scalar U+00E9, the letter e with an acute accent. This is the shortest encoding of that value, because anything below U+0080 would fit in one byte and U+00E9 does not. The decoder returns the character, which shows that the tool works normally on valid input. Keeping a control like this next to failing cases helps confirm that an error comes from the sequence and not from a formatting problem such as separators.
Overlong encodings: C0 AF
The bytes C0 AF look like a two-byte sequence. C0 is 11000000 and AF is 10101111, and joining the payload bits gives 0101111, which is 0x2F, the slash. A naive decoder would return a slash. But the slash is a one-byte character, 2F, and encoding it in two bytes is an overlong form. RFC 3629 forbids overlong encodings, and the lead bytes C0 and C1 can never appear in valid UTF-8 because they could only start overlong two-byte sequences. The concern is practical as well as formal. Overlong forms of characters like the slash and the dot were used in directory traversal attacks, such as one against Microsoft IIS around 2000, because filters checking for the plain byte missed the longer spelling.
Surrogate encodings: ED A0 80
The bytes ED A0 80 have a valid three-byte pattern. ED is 11101101, A0 is 10100000 and 80 is 10000000. Joining the payload bits gives 1101 100000 000000, which is 0xD800, the first high surrogate. UTF-8 is not allowed to encode surrogate code points, so the sequence is invalid even though every byte fits the bit pattern. Some systems produce bytes like these on purpose. CESU-8 encodes each UTF-16 surrogate of a supplementary character as its own three-byte sequence, and Java's modified UTF-8 does the same. Neither is UTF-8, and a strict decoder will reject them. The valid UTF-8 encoding of a supplementary character such as U+1F600 is the four bytes F0 9F 98 80.
The shape of each rejection
The three byte lists in this guide fail in different ways, and each has a characteristic repair question. A truncated sequence, covered in a separate guide, ends before its continuation bytes arrive. A stray continuation byte such as 80 on its own has no lead. An overlong sequence has a complete shape and a value that should have been written shorter. A surrogate encoding has a complete shape and a value in the forbidden range. Bytes F5 to FF can never start a sequence, since they would imply values beyond U+10FFFF. A decoder that reports these as one family of errors is behaving correctly. The message tells you the byte list is not valid UTF-8, and the classes tell you where to look.
Why not substitute a replacement character
Many decoders handle bad input by emitting U+FFFD, the replacement character, and carrying on. The WHATWG Encoding Standard, which browsers follow, specifies a precise replacement procedure for that purpose. That behavior is convenient when displaying untrusted text, and it makes errors invisible when you are inspecting data. This converter rejects the input instead, so a single bad sequence is not hidden among otherwise readable output. For debugging, that is the more useful choice: you see that something is wrong, and you keep the raw bytes available for diagnosis. For display of web content, replacement is the normal practice and is not wrong, but it answers a different need.
Reading hex input correctly
The decode mode takes bytes written as hexadecimal pairs separated by spaces, for example C3 A9. Each pair is one byte, so C3A9 and C3 A9 name the same two bytes, while C3 A is a malformed attempt. Case does not matter for the hex digits. Before concluding that data is corrupt, check that you copied whole bytes and in the right order, since reversing the pairs of a valid sequence produces a different and usually invalid list. Another common source of confusion is text that has been decoded once with the wrong character set. The famous é displayed instead of é happens because the two bytes C3 A9 were read as two Latin-1 characters instead of one UTF-8 character.
Enumerating the shortest-form ranges
Each length has a minimum scalar value that justifies it, and that is how a decoder detects overlongs. Two-byte sequences must decode to U+0080 or higher, three-byte sequences to U+0800 or higher, and four-byte sequences to U+10000 or higher. A three-byte sequence starting with E0 therefore needs its second byte in the range A0 to BF, and a four-byte sequence starting with F0 needs 90 to BF. At the top end, F4 must be followed by 80 to 8F so the value stays at or below U+10FFFF. After ED, the second byte must lie between 80 and 9F, which is the rule that excludes the surrogate range. Table 3-7 of the Unicode Standard lists these well-formed byte patterns.
What the three cases settle
The cases show that this decoder accepts a valid two-byte scalar and rejects an overlong two-byte sequence and a three-byte surrogate encoding, with an error that says the input is not valid UTF-8. They do not tell you what the author intended with a rejected byte list, and the decoder does not guess. Where the bytes came from matters: a legacy Latin-1 file, a file with a different encoding label, a truncated download or a system using CESU-8 each needs a different fix. Keep the original bytes, identify the producer and decide on a repair rule there. This worksheet demonstrates parser behavior only and offers no recovery procedure.
Reproduce the checks
Valid two-byte control
Open the Utf8 Byte Converter and enter:
- Text or encoded input
C3 A9- Calculation mode
decode
Expected output:
Hex
éThe sequence represents a valid scalar.
Overlong sequence
Open the Utf8 Byte Converter and enter:
- Text or encoded input
C0 AF- Calculation mode
decode
Expected rejection:
not valid UTF-8A complete byte count does not make an overlong sequence valid.
Surrogate-range encoding
Open the Utf8 Byte Converter and enter:
- Text or encoded input
ED A0 80- Calculation mode
decode
Expected rejection:
not valid UTF-8Surrogate values are not valid UTF-8 scalar encodings.
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
- Primary definition and convention reference ↗ · 2026-10-08