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:
在上面的示例中,我们首先创建了一个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:
在上面的示例中,我们使用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:
在上面的示例中,我们首先引入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:
在上面的示例中,我们直接调用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:
在上面的示例中,我们首先引入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:
在上面的示例中,我们使用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:
在上面的示例中,我们首先引入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:
在上面的示例中,我们使用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:
在上面的示例中,我们首先引入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:
在上面的示例中,我们使用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:
在上面的示例中,我们首先引入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:
在上面的示例中,我们使用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:
在上面的示例中,我们首先引入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:
在上面的示例中,我们使用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:
在上面的示例中,我们首先引入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:
在上面的示例中,我们使用Uint8Array.reduce
方法将Uint8Array类型的Byte数组bytes
转换为String。