happy-codec
    Preparing search index...

    Function decodeUtf8

    • Decodes binary data to string (UTF-8 decoding).

      Uses native TextDecoder when available (faster at all input sizes). Falls back to pure JS when TextDecoder is not available.

      Parameters

      • data: BufferSource

        The binary data to decode.

      • Optionaloptions: TextDecoderOptions

        Decoding options (same as TextDecoderOptions).

        • fatal

          If true, throw on invalid sequences. If false (default), replace with U+FFFD.

        • ignoreBOM

          If true, keep BOM in output. If false (default), strip BOM.

      Returns string

      Decoded string.

      If options.fatal is true and the input contains invalid UTF-8 sequences.

      1.0.0

      const decoded = decodeUtf8(new Uint8Array([228, 189, 160, 229, 165, 189]));
      console.log(decoded); // '你好'

      // With invalid bytes (non-fatal, default)
      const withReplacement = decodeUtf8(new Uint8Array([0xff, 0xfe]));
      console.log(withReplacement); // '��'

      // With invalid bytes (fatal)
      decodeUtf8(new Uint8Array([0xff, 0xfe]), { fatal: true }); // throws Error

      // BOM handling (default: strip BOM)
      const withBOM = new Uint8Array([0xef, 0xbb, 0xbf, 0x48, 0x69]); // BOM + 'Hi'
      decodeUtf8(withBOM); // 'Hi'
      decodeUtf8(withBOM, { ignoreBOM: true }); // '\uFEFFHi'