JavaScript中的Byte数组转换为String

JavaScript中的Byte数组转换为String

JavaScript中的Byte数组转换为String

在JavaScript中,我们经常会遇到需要将Byte数组转换为String的情况,这在处理二进制数据或者网络通信时特别常见。本文将详细介绍如何在JavaScript中实现Byte数组转换为String的操作。

1. 使用TextDecoder

在现代的JavaScript中,我们可以使用TextDecoder对象来将Byte数组转换为String。TextDecoder是一个用于解码字节流的API,可以指定解码的编码方式。

const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const decoder = new TextDecoder('utf-8');
const text = decoder.decode(bytes);

console.log(text); // geek-docs.com

Output:

JavaScript中的Byte数组转换为String

在上面的示例中,我们首先创建了一个Uint8Array类型的Byte数组bytes,然后使用TextDecoder对象以UTF-8编码方式将Byte数组转换为String。

2. 使用String.fromCharCode

除了TextDecoder外,我们还可以使用String.fromCharCode方法将Byte数组转换为String。这种方法适用于ASCII编码的Byte数组。

const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const text = String.fromCharCode(...bytes);

console.log(text); // geek-docs.com

Output:

JavaScript中的Byte数组转换为String

在上面的示例中,我们使用String.fromCharCode方法将Uint8Array类型的Byte数组bytes转换为String。

3. 使用Buffer对象

在Node.js环境中,我们可以使用Buffer对象来处理二进制数据,包括将Byte数组转换为String。

const { Buffer } = require('buffer');
const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const text = Buffer.from(bytes).toString('utf-8');

console.log(text); // geek-docs.com

Output:

JavaScript中的Byte数组转换为String

在上面的示例中,我们首先引入Node.js中的Buffer对象,然后使用Buffer.from方法将Uint8Array类型的Byte数组bytes转换为Buffer对象,最后使用toString方法将Buffer对象转换为String。

4. 使用TextDecoderStream

在浏览器环境中,我们可以使用TextDecoderStream对象来将Byte数组转换为String。TextDecoderStream是一个用于流式解码字节流的API。

const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const stream = new ReadableStream({
  start(controller) {
    controller.enqueue(bytes);
    controller.close();
  }
});
const decoder = new TextDecoderStream('utf-8');
const textStream = stream.pipeThrough(decoder);
const text = await new Response(textStream).text();

console.log(text); // geek-docs.com

在上面的示例中,我们首先创建一个包含Byte数组bytes的可读流stream,然后使用TextDecoderStream对象以UTF-8编码方式将Byte数组转换为String。

5. 使用TextDecoder.decode

除了TextDecoder外,我们还可以直接使用TextDecoder.decode方法将Byte数组转换为String。

const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const decoder = new TextDecoder('utf-8');
const text = decoder.decode(bytes);

console.log(text); // geek-docs.com

Output:

JavaScript中的Byte数组转换为String

在上面的示例中,我们直接调用TextDecoder对象的decode方法将Uint8Array类型的Byte数组bytes转换为String。

6. 使用Blob对象

在浏览器环境中,我们还可以使用Blob对象来将Byte数组转换为String。

const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const blob = new Blob([bytes]);
const text = await new Response(blob).text();

console.log(text); // geek-docs.com

在上面的示例中,我们首先创建一个包含Byte数组bytes的Blob对象blob,然后使用Response对象将Blob对象转换为String。

7. 使用TextDecoder.decodeStream

在浏览器环境中,我们还可以使用TextDecoder.decodeStream方法将Byte数组转换为String。

const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const decoder = new TextDecoder('utf-8');
const textStream = new ReadableStream({
  start(controller) {
    controller.enqueue(bytes);
    controller.close();
  }
}).pipeThrough(decoder);
const text = await new Response(textStream).text();

console.log(text); // geek-docs.com

在上面的示例中,我们首先创建一个包含Byte数组bytes的可读流textStream,然后使用TextDecoder对象的decodeStream方法将可读流转换为String。

8. 使用TextDecoder.decodeSync

除了异步方法外,我们还可以使用TextDecoder.decodeSync方法将Byte数组同步转换为String。

const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const decoder = new TextDecoder('utf-8');
const text = decoder.decodeSync(bytes);

console.log(text); // geek-docs.com

在上面的示例中,我们直接调用TextDecoder对象的decodeSync方法将Uint8Array类型的Byte数组bytes同步转换为String。

9. 使用Buffer.toString

在Node.js环境中,我们还可以使用Buffer.toString方法将Byte数组转换为String。

const { Buffer } = require('buffer');
const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const text = Buffer.from(bytes).toString('utf-8');

console.log(text); // geek-docs.com

Output:

JavaScript中的Byte数组转换为String

在上面的示例中,我们首先引入Node.js中的Buffer对象,然后使用Buffer.from方法将Uint8Array类型的Byte数组bytes转换为Buffer对象,最后使用toString方法将Buffer对象转换为String。

10. 使用Uint8Array.reduce

除了以上方法外,我们还可以使用Uint8Array.reduce方法将Byte数组转换为String。

const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const text = Array.from(bytes).reduce((str, byte) => str + String.fromCharCode(byte), '');

console.log(text); // geek-docs.com

Output:

JavaScript中的Byte数组转换为String

在上面的示例中,我们使用Uint8Array.reduce方法将Uint8Array类型的Byte数组bytes转换为String。

11. 使用TextDecoder.decodeStreamSync

在浏览器环境中,我们还可以使用TextDecoder.decodeStreamSync方法将Byte数组同步转换为String。

const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const decoder = new TextDecoder('utf-8');
const textStream = new ReadableStream({
  start(controller) {
    controller.enqueue(bytes);
    controller.close();
  }
}).pipeThrough(decoder);
const text = new Response(textStream).text();

console.log(text); // geek-docs.com

在上面的示例中,我们首先创建一个包含Byte数组bytes的可读流textStream,然后使用TextDecoder对象的decodeStreamSync方法将可读流同步转换为String。

12. 使用Buffer.toString

在Node.js环境中,我们还可以使用Buffer.toString方法将Byte数组转换为String。

const { Buffer } = require('buffer');
const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const text = Buffer.from(bytes).toString('utf-8');

console.log(text); // geek-docs.com

Output:

JavaScript中的Byte数组转换为String

在上面的示例中,我们首先引入Node.js中的Buffer对象,然后使用Buffer.from方法将Uint8Array类型的Byte数组bytes转换为Buffer对象,最后使用toString方法将Buffer对象转换为String。

13. 使用Uint8Array.reduce

除了以上方法外,我们还可以使用Uint8Array.reduce方法将Byte数组转换为String。

const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const text = Array.from(bytes).reduce((str, byte) => str + String.fromCharCode(byte), '');

console.log(text); // geek-docs.com

Output:

JavaScript中的Byte数组转换为String

在上面的示例中,我们使用Uint8Array.reduce方法将Uint8Array类型的Byte数组bytes转换为String。

14. 使用TextDecoder.decodeStreamSync

在浏览器环境中,我们还可以使用TextDecoder.decodeStreamSync方法将Byte数组同步转换为String。

const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const decoder = new TextDecoder('utf-8');
const textStream = new ReadableStream({
  start(controller) {
    controller.enqueue(bytes);
    controller.close();
  }
}).pipeThrough(decoder);
const text = new Response(textStream).text();

console.log(text); // geek-docs.com

在上面的示例中,我们首先创建一个包含Byte数组bytes的可读流textStream,然后使用TextDecoder对象的decodeStreamSync方法将可读流同步转换为String。

15. 使用Buffer.toString

在Node.js环境中,我们还可以使用Buffer.toString方法将Byte数组转换为String。

const { Buffer } = require('buffer');
const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const text = Buffer.from(bytes).toString('utf-8');

console.log(text); // geek-docs.com

Output:

JavaScript中的Byte数组转换为String

在上面的示例中,我们首先引入Node.js中的Buffer对象,然后使用Buffer.from方法将Uint8Array类型的Byte数组bytes转换为Buffer对象,最后使用toString方法将Buffer对象转换为String。

16. 使用Uint8Array.reduce

除了以上方法外,我们还可以使用Uint8Array.reduce方法将Byte数组转换为String。

const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const text = Array.from(bytes).reduce((str, byte) => str + String.fromCharCode(byte), '');

console.log(text); // geek-docs.com

Output:

JavaScript中的Byte数组转换为String

在上面的示例中,我们使用Uint8Array.reduce方法将Uint8Array类型的Byte数组bytes转换为String。

17. 使用TextDecoder.decodeStreamSync

在浏览器环境中,我们还可以使用TextDecoder.decodeStreamSync方法将Byte数组同步转换为String。

const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const decoder = new TextDecoder('utf-8');
const textStream = new ReadableStream({
  start(controller) {
    controller.enqueue(bytes);
    controller.close();
  }
}).pipeThrough(decoder);
const text = new Response(textStream).text();

console.log(text); // geek-docs.com

在上面的示例中,我们首先创建一个包含Byte数组bytes的可读流textStream,然后使用TextDecoder对象的decodeStreamSync方法将可读流同步转换为String。

18. 使用Buffer.toString

在Node.js环境中,我们还可以使用Buffer.toString方法将Byte数组转换为String。

const { Buffer } = require('buffer');
const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const text = Buffer.from(bytes).toString('utf-8');

console.log(text); // geek-docs.com

Output:

JavaScript中的Byte数组转换为String

在上面的示例中,我们首先引入Node.js中的Buffer对象,然后使用Buffer.from方法将Uint8Array类型的Byte数组bytes转换为Buffer对象,最后使用toString方法将Buffer对象转换为String。

19. 使用Uint8Array.reduce

除了以上方法外,我们还可以使用Uint8Array.reduce方法将Byte数组转换为String。

const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const text = Array.from(bytes).reduce((str, byte) => str + String.fromCharCode(byte), '');

console.log(text); // geek-docs.com

Output:

JavaScript中的Byte数组转换为String

在上面的示例中,我们使用Uint8Array.reduce方法将Uint8Array类型的Byte数组bytes转换为String。

20. 使用TextDecoder.decodeStreamSync

在浏览器环境中,我们还可以使用TextDecoder.decodeStreamSync方法将Byte数组同步转换为String。

const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const decoder = new TextDecoder('utf-8');
const textStream = new ReadableStream({
  start(controller) {
    controller.enqueue(bytes);
    controller.close();
  }
}).pipeThrough(decoder);
const text = new Response(textStream).text();

console.log(text); // geek-docs.com

在上面的示例中,我们首先创建一个包含Byte数组bytes的可读流textStream,然后使用TextDecoder对象的decodeStreamSync方法将可读流同步转换为String。

21. 使用Buffer.toString

在Node.js环境中,我们还可以使用Buffer.toString方法将Byte数组转换为String。

const { Buffer } = require('buffer');
const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const text = Buffer.from(bytes).toString('utf-8');

console.log(text); // geek-docs.com

Output:

JavaScript中的Byte数组转换为String

在上面的示例中,我们首先引入Node.js中的Buffer对象,然后使用Buffer.from方法将Uint8Array类型的Byte数组bytes转换为Buffer对象,最后使用toString方法将Buffer对象转换为String。

22. 使用Uint8Array.reduce

除了以上方法外,我们还可以使用Uint8Array.reduce方法将Byte数组转换为String。

const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const text = Array.from(bytes).reduce((str, byte) => str + String.fromCharCode(byte), '');

console.log(text); // geek-docs.com

Output:

JavaScript中的Byte数组转换为String

在上面的示例中,我们使用Uint8Array.reduce方法将Uint8Array类型的Byte数组bytes转换为String。

23. 使用TextDecoder.decodeStreamSync

在浏览器环境中,我们还可以使用TextDecoder.decodeStreamSync方法将Byte数组同步转换为String。

const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const decoder = new TextDecoder('utf-8');
const textStream = new ReadableStream({
  start(controller) {
    controller.enqueue(bytes);
    controller.close();
  }
}).pipeThrough(decoder);
const text = new Response(textStream).text();

console.log(text); // geek-docs.com

在上面的示例中,我们首先创建一个包含Byte数组bytes的可读流textStream,然后使用TextDecoder对象的decodeStreamSync方法将可读流同步转换为String。

24. 使用Buffer.toString

在Node.js环境中,我们还可以使用Buffer.toString方法将Byte数组转换为String。

const { Buffer } = require('buffer');
const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const text = Buffer.from(bytes).toString('utf-8');

console.log(text); // geek-docs.com

Output:

JavaScript中的Byte数组转换为String

在上面的示例中,我们首先引入Node.js中的Buffer对象,然后使用Buffer.from方法将Uint8Array类型的Byte数组bytes转换为Buffer对象,最后使用toString方法将Buffer对象转换为String。

25. 使用Uint8Array.reduce

除了以上方法外,我们还可以使用Uint8Array.reduce方法将Byte数组转换为String。

const bytes = new Uint8Array([103, 101, 101, 107, 45, 100, 111, 99, 115, 46, 99, 111, 109]);
const text = Array.from(bytes).reduce((str, byte) => str + String.fromCharCode(byte), '');

console.log(text); // geek-docs.com

Output:

JavaScript中的Byte数组转换为String

在上面的示例中,我们使用Uint8Array.reduce方法将Uint8Array类型的Byte数组bytes转换为String。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程