UTF8 decode JavaScript means converting a UTF-8 byte sequence into a JavaScript string; using TextDecoder with the fatal option makes malformed bytes raise an error instead of becoming U+FFFD replacement characters.

Use the browser's native TextDecoder when your code already has a Uint8Array. If the bytes are represented as hexadecimal, decimal, or binary text, the UTF-8 Encoder / Decoder can inspect that notation, but it does not accept a raw Uint8Array or file upload.

utf8 decode javascript
UTF8 Decode JavaScript: Strict Bytes-to-Text Guide

Decode UTF-8 bytes in JavaScript

A JavaScript UTF-8 decoder receives bytes, not a Unicode code-point list. The input must normally be a Uint8Array or another byte-oriented ArrayBuffer view. The following process decodes a complete sequence and makes malformed data visible.

  1. Obtain a Uint8Array. A response body can be converted with new Uint8Array(await response.arrayBuffer()). For a short known test, use const bytes = new Uint8Array([0xE2, 0x82, 0xAC]);. Replace this sample with the actual bytes being handled.
  2. Create a strict decoder. Use const decoder = new TextDecoder("utf-8", { fatal: true });. The fatal option prevents damaged bytes from being silently replaced during decoding.
  3. Decode inside error-handling logic. A practical call is try { const text = decoder.decode(bytes); console.log(text); } catch (error) { console.error("Invalid UTF-8:", error); }. A strict decoder reports malformed input rather than returning a plausible-looking string containing replacement characters.
  4. Handle chunk boundaries when necessary. If network or file data arrives in chunks, call decoder.decode(chunk, { stream: true }) for each chunk, then call decoder.decode() after the final chunk. This allows the decoder to retain an incomplete multi-byte sequence until its remaining bytes arrive.

In the known sample above, the three bytes decode to the euro sign, €. Because the decoder is strict, a truncated or otherwise invalid version of that sequence fails rather than producing misleading text.

Choose strict decoding deliberately

TextDecoder supports two practical error policies. Select one based on whether the result needs to be exact or is only intended for a best-effort display.

Decoding mode Configuration Malformed bytes Appropriate use
Strict new TextDecoder("utf-8", { fatal: true }) Raise an error Validation, data processing, and verified round trips
Replacement new TextDecoder("utf-8") Insert U+FFFD replacement characters Best-effort previews where damaged data is acceptable

Strict decoding rejects overlong encodings, truncated sequences, isolated continuation bytes, encoded surrogate values, and values beyond the valid Unicode range. Replacement mode can make corrupted data appear readable, but U+FFFD does not prove what the original characters were. For validation workflows, an explicit error is safer than silently altered text.

The same distinction applies when inspecting byte notation online. A byte sequence has no visible meaning until its encoding is known, and a replacement character cannot reveal whether the source was truncated, incorrectly transcribed, or produced under a different encoding.

Prepare hexadecimal, decimal, and binary input

If your data is already a textual byte representation, open the UTF-8 Encoder / Decoder and select the UTF-8-bytes-to-text operation. These notations contain the same underlying byte array; only the written representation changes.

  • Hexadecimal: Decoding accepts one continuous even-length hexadecimal string. It also accepts space- or comma-separated one- or two-digit byte tokens, with optional 0x prefixes on separated tokens. When encoding text, the hexadecimal output uses uppercase two-digit bytes separated by spaces.
  • Decimal: Input consists of integer tokens only, with each byte ranging from 0 through 255. Encoded output uses the same byte values separated by spaces.
  • Binary: Every binary token must contain exactly eight zero-or-one characters. Encoded output follows the same eight-bits-per-byte format.

Empty input decodes to empty text. Spaces, commas, prefixes, and token length are part of the input contract, so remove or add notation only when you intend to change the source representation. Changing from hexadecimal to binary does not modify the UTF-8 byte sequence itself.

These formats should not be confused with Unicode code-point notation, UTF-16 code units, HTML entities, URL percent encoding, Base64, encryption, or compression. For example, hexadecimal and binary are two views of the same bytes, while Base64 and percent encoding are transport representations used for different purposes.

Convert and verify in the browser

The browser-based converter provides a practical way to inspect and verify byte notation without uploading the data.

  1. Choose the operation and notation. Select either Text to UTF-8 bytes or UTF-8 bytes to text, then choose hexadecimal, decimal, or binary output or input notation.
  2. Enter the source data. Paste Unicode text for encoding or strictly formatted byte tokens for decoding. Confirm that the selected notation matches the actual source; a valid byte sequence in one notation may be invalid text in another.
  3. Run the conversion. Select the conversion button. A malformed sequence produces an explicit error rather than a decoded string containing silent replacements.
  4. Compare the exact output. Inspect every byte boundary and compare the result with the source format. For hexadecimal output, check the uppercase two-digit grouping; for binary output, confirm that each byte still contains eight bits.
  5. Verify a round trip before replacing data. Decode the notation to text, reverse the operation using the same notation, and compare the new bytes with the original byte notation. Preserve the source until that comparison succeeds.

All conversion work stays in the current browser tab. The tool does not upload, log, store, normalize, translate, escape, or automatically copy the data. Its input is bounded at 200,000 UTF-16 code units for text and 200,000 bytes for decoded notation. It also does not stream large files or accept uploads, so use a dedicated binary workflow for large data.

Verify a JavaScript round trip

When data must survive an encode-and-decode cycle, compare the final string with the original instead of relying only on visual inspection.

const original = inputText; const bytes = new TextEncoder().encode(original); const restored = new TextDecoder("utf-8", { fatal: true }).decode(bytes); if (restored !== original) throw new Error("UTF-8 round trip failed");

TextEncoder returns UTF-8 bytes in a Uint8Array. The strict TextDecoder then converts those bytes back into a JavaScript string. This check is particularly important for JavaScript strings containing an unpaired UTF-16 surrogate code unit: the platform encoder normally replaces such a fragment with U+FFFD, so the original and restored strings will not match.

A valid surrogate pair representing a supplementary character is accepted and encoded correctly. The browser tool handles the same risk more defensively by rejecting unpaired UTF-16 surrogate code units before calling TextEncoder. Application code that claims lossless conversion should perform an equivalent check or rely on the round-trip comparison.

If decoding fails, keep the original bytes and investigate their source. Switching to replacement mode may reveal more characters, but it will not prove that those characters were present in the original data.

Keep UTF-8 separate from other representations

UTF-8 maps Unicode scalar values to bytes. Valid scalar values use one through four bytes, the permitted range ends at U+10FFFF, and UTF-16 surrogate code points are excluded. The byte ranges are defined by RFC 3629, while the scalar-value model is covered in the Unicode Core Specification, Chapter 3.

A JavaScript string is a sequence of UTF-16 code units, so string.length does not report UTF-8 byte count. A supplementary emoji, for example, is one Unicode code point and four UTF-8 bytes but occupies two JavaScript UTF-16 code units. This distinction matters when validating API payloads, calculating storage size, or comparing a string with its encoded form.

The converter assumes UTF-8 and does not auto-detect legacy encodings such as Windows-1252, Shift JIS, GBK, or the ISO-8859 families. If valid-looking text fails to decode, identify the original encoding rather than forcing the bytes through UTF-8. Preserve unknown source data first, then convert with the correct encoding and verify the resulting bytes.

For a deeper look, see XOR Encryption Online on Windows: Exact Byte Workflow.

For a deeper look, see ASCII to Text: Decode Decimal Codes Without Guesswork.