收藏718
分享
阅读7465
更新时间2025-08-28
Node.js –将数组转换为缓冲区:要将数组(八位字节数组/数字数组/二进制数组)转换为缓冲区,请使用Buffer。from(array)方法。
Buffer. |
Buffer.from方法从数组中读取八位位组,并返回使用这些读取字节初始化的缓冲区。
在下面的示例中,八位字节数组被读取到缓冲区。
var arr = [0x74, 0x32, 0x91];
const buf = Buffer.from(arr);
for(const byt of buf.values()){
console.log(byt);
}输出结果
$ node array-to-buffer.js 116 50 145
我们已将每个字节中的数据记录为数字。
0x74 = 0111 0100 = 116 0x32 = 0011 0010 = 50 0x91 = 1001 0001 = 145
在以下示例中,将数字数组读取到缓冲区。
var arr = [74, 32, 91];
const buf = Buffer.from(arr);
for(const byt of buf.values()){
console.log(byt);
}输出结果
$ node array-to-buffer.js 74 32 91
我们已将每个字节中的数据记录为数字。
在下面的示例中,八位字节数组被读取到缓冲区。
var arr = [true, true, false];
const buf = Buffer.from(arr);
for(const byt of buf.values()){
console.log(byt);
}输出结果
$ node array-to-buffer.js 1 1 0
true为1,false为0。
在本Node.js教程– Node.js将数组转换为缓冲区中,我们学习了如何将八位字节数组,数字数组和布尔数组转换为Node.js缓冲区。
相关
视频
RELATED VIDEOS
科技资讯
1
2
3
4
5
6
7
8
9
精选课程
共5课时
17.2万人学习
共49课时
77万人学习
共29课时
61.7万人学习
共25课时
39.3万人学习
共43课时
71万人学习
共25课时
61.6万人学习
共22课时
23万人学习
共28课时
33.9万人学习
共89课时
125万人学习