minigame-std
    Preparing search index...

    Function decodeUtf8

    • 将二进制数据解码为字符串(UTF-8 解码)。

      Parameters

      • data: BufferSource

        需要解码的二进制数据。

      • Optionaloptions: TextDecoderOptions

        解码选项(可选)。

        • fatal

          如果为 true,遇到无效的 UTF-8 序列会抛出异常;如果为 false(默认),使用替换字符 U+FFFD 代替。

        • ignoreBOM

          如果为 true,保留字节顺序标记(BOM);如果为 false(默认),自动删除 BOM。

      Returns string

      解码后的字符串。

      options.fataltrue 且输入包含无效的 UTF-8 序列时。

      1.0.0

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

      // 使用 fatal 选项处理无效字节
      const withReplacement = decodeUtf8(new Uint8Array([0xff, 0xfe]));
      console.log(withReplacement); // '��'(使用替换字符)

      // 使用 ignoreBOM 选项保留 BOM
      const withBOM = new Uint8Array([0xef, 0xbb, 0xbf, 0x48, 0x69]); // BOM + 'Hi'
      decodeUtf8(withBOM); // 'Hi'(删除 BOM)
      decodeUtf8(withBOM, { ignoreBOM: true }); // '\uFEFFHi'(保留 BOM)