In JavaScript, decode UTF-8 bytes with new TextDecoder("utf-8", { fatal: true }).decode(bytes) so malformed sequences throw instead of silently becoming replacement characters.
A UTF8 decode JS workflow usually starts with bytes received from an API, a file, or a byte-oriented log. The decoder needs a byte container such as a Uint8Array or ArrayBuffer, not an array of Unicode code points treated as if every number were one UTF-8 byte. This distinction matters for accented characters, symbols, and emoji, whose UTF-8 representations can occupy multiple bytes.

Decode UTF-8 bytes in JavaScript
Use the browser’s standard TextDecoder API when the source is already binary UTF-8. The fatal: true option changes malformed-input handling: the decoder raises an error instead of accepting damaged bytes and inserting U+FFFD replacement characters.
- Confirm the source is bytes. A Uint8Array is suitable. If the source is hexadecimal, decimal, or binary notation, parse that notation into numeric byte values first.
- Create a fatal decoder. Use new TextDecoder("utf-8", { fatal: true }) rather than relying on the default replacement behavior.
- Decode the byte container. Pass the Uint8Array or ArrayBuffer to the decoder’s decode method.
- Handle failure explicitly. Catch the error at the point where the bytes enter your application, report the invalid input, and avoid saving or forwarding a partially decoded result.
Here is a complete example using the UTF-8 bytes for the euro sign:
const bytes = new Uint8Array([0xE2, 0x82, 0xAC]);
const decoder = new TextDecoder("utf-8", { fatal: true });
try {
const text = decoder.decode(bytes);
console.log(text); // €
} catch (error) {
console.error("Invalid UTF-8:", error);
}
The three supplied bytes form one valid UTF-8 sequence, so the decoder returns the euro sign. If a sequence is incomplete, overlong, contains an isolated continuation byte, or otherwise violates UTF-8, fatal mode stops the operation instead of making damaged data look verified.
JavaScript strings also have their own internal model. They consist of UTF-16 code units, while UTF-8 uses one to four bytes for each Unicode scalar value. A supplementary emoji can therefore be one Unicode code point, four UTF-8 bytes, and typically two JavaScript UTF-16 code units at the same time. These are related measurements, not interchangeable values.
For a reverse check, use new TextEncoder().encode(text) to produce a Uint8Array and then decode those bytes with the fatal decoder. Do not build UTF-8 bytes by adding JavaScript character codes manually. A native TextEncoder normally replaces an unpaired UTF-16 surrogate with U+FFFD, so an ill-formed JavaScript string is not a lossless source. The browser converter described below rejects unpaired surrogates before encoding, while valid surrogate pairs representing supplementary characters are accepted.
Prepare hexadecimal, decimal, and binary bytes
Byte notation is only a display form of the same underlying array. Before calling TextDecoder, turn each notation into a Uint8Array and keep every byte in the 0–255 range. The accepted formats are deliberately strict so that a malformed token cannot quietly become a different byte.
| Input notation | Accepted format | Output representation |
|---|---|---|
| Hexadecimal | One continuous even-length hexadecimal string, or space- or comma-separated one- or two-digit byte tokens. Separated tokens may use 0x prefixes. | Uppercase two-digit bytes separated by spaces. |
| Decimal | Integer tokens only, with each byte from 0 through 255. | Decimal values from 0 through 255 separated by spaces. |
| Binary | Tokens containing exactly eight zero-or-one characters. | Exactly eight bits per byte. |
| Empty | No tokens. | Empty text when decoding. |
Changing the notation changes how the bytes are displayed, not their UTF-8 sequence. A hexadecimal byte, its decimal value, and its eight-bit binary form are three views of the same number.
For separated hexadecimal input, a small parser can validate the tokens before decoding:
function hexToBytes(input) {
const source = input.trim();
let values = [];
if (/^[0-9A-Fa-f]+$/.test(source)) {
if (source.length % 2 !== 0) {
throw new Error("Continuous hexadecimal needs an even number of digits.");
}
for (let i = 0; i < source.length; i += 2) {
values.push(Number.parseInt(source.slice(i, i + 2), 16));
}
} else {
values = source.split(/[\s,]+/).filter(Boolean).map((token) => {
const clean = token.replace(/^0x/i, "");
if (!/^[0-9A-Fa-f]{1,2}$/.test(clean)) {
throw new Error("Use one- or two-digit hexadecimal byte tokens.");
}
return Number.parseInt(clean, 16);
});
}
return Uint8Array.from(values);
}
const text = new TextDecoder("utf-8", { fatal: true }).decode(hexToBytes(hexInput));
Decimal input should be parsed as integers and checked for the valid byte range. Binary input should be split into eight-character groups. Once either notation becomes a Uint8Array, the JavaScript decoding step is the same: call the fatal TextDecoder.
Decode a byte notation in the browser
The UTF-8 Encoder / Decoder provides a local workflow for checking text, hexadecimal, decimal, and 8-bit binary data without writing a parser first.
- Open the converter and choose a direction. Select UTF-8 bytes to text for decoding, or Text to UTF-8 bytes for encoding. Then choose hexadecimal, decimal, or binary notation.
- Enter the source in the required format. Paste Unicode text when encoding, or strictly formatted byte tokens when decoding. Select the conversion button to run the operation.
- Inspect the result or error. Valid input produces text or bytes in the selected notation. Malformed UTF-8 receives an explicit error rather than replacement characters. Empty input decodes to empty text.
- Verify a round trip. Encode the decoded text back into bytes using the same notation, compare the resulting byte sequence with the original, and preserve the source until the comparison succeeds.
All processing happens in the current browser tab. The converter does not upload, log, or store the text or bytes, and it does not automatically copy or transform the result for another context. The result panel reports bytes or code points according to the selected operation, which helps distinguish a character count from a byte count.
Handle malformed UTF-8 as an error
UTF-8 defines strict byte patterns, not an informal sequence of character numbers. The rules are described in RFC 3629, while the relationship between scalar values and code points is covered in the Unicode Core Specification, Chapter 3. Fatal decoding applies those rules instead of hiding a mismatch behind replacement characters.
| Input condition | Strict behavior |
|---|---|
| Valid UTF-8 byte sequence | Returns the corresponding text. |
| Overlong encoding | Rejected with an explicit error. |
| Truncated multibyte sequence | Rejected with an explicit error. |
| Isolated continuation byte | Rejected with an explicit error. |
| Encoded surrogate value | Rejected with an explicit error. |
| Value above U+10FFFF | Rejected with an explicit error. |
| Unpaired UTF-16 surrogate in text being encoded | Rejected before TextEncoder can replace it with U+FFFD. |
Error handling belongs at the boundary where bytes arrive. Log enough context to identify the failed source, but do not assume the decoder produced trustworthy text after an exception. If the operation succeeds, the returned string represents a validated UTF-8 sequence rather than a mixture of original text and inserted replacement characters.
Know the limits and avoid false certainty
The browser converter accepts up to 200,000 UTF-16 code units for text input and 200,000 bytes for decoded notation. It does not stream large files or accept uploads, so a dedicated binary tool is more appropriate for large data. Its strict parsing also means that input notation must be complete; a token that looks hexadecimal is not accepted if it contains invalid characters or an invalid byte length.
UTF-8 is not the same as Unicode code points, UTF-16 code units, HTML entities, URL percent encoding, Base64, encryption, or compression. Base64 and percent encoding transport bytes in different contexts, while UTF-8 defines how Unicode scalar values map to bytes. If the source is Base64, decode that transport layer first and then apply UTF-8 decoding to the resulting bytes.
The converter assumes UTF-8 and does not auto-detect Windows-1252, Shift JIS, GBK, or other legacy encodings. A byte sequence has no visible meaning until its encoding is known. If strict UTF-8 decoding fails, identify the original encoding rather than forcing the bytes through UTF-8.
For a reliable JavaScript workflow, preserve the original bytes, begin with a short known sample, inspect the byte boundaries, and complete an exact round trip before replacing any source data. The converter’s local processing helps protect confidentiality, but correctness still depends on confirming both the source encoding and the expected byte sequence.