nodejs 从缓冲区数据到字节数组的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51511307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
nodejs conversion from buffer data to byte array
提问by lilKing
I want to convert buffer data to byte array. Here's what I've tried
我想将缓冲区数据转换为字节数组。这是我尝试过的
import * as fs from 'fs';
[...]
event:(data) => {
fs.readFile(data, function(err, data) {
var arrByte= new Uint8Array(data)
var binaryData= new Blob([arrByte])
console.log(binaryData)
}
}
I'm yet to have this work hence my post. I'd very much like to know what I'm doing that's not right.
我还没有完成这项工作,因此我的帖子。我很想知道我在做什么,这是不对的。
回答by josh3736
The Bufferdocs are very enlightening:
Prior to the introduction of
TypedArray, the JavaScript language had no mechanism for reading or manipulating streams of binary data. TheBufferclass was introduced as part of the Node.js API to enable interaction with octet streams in TCP streams, file system operations, and other contexts.With
TypedArraynow available, theBufferclass implements theUint8ArrayAPI in a manner that is more optimized and suitable for Node.js.…
Buffer instances are also
Uint8Arrayinstances.However, there are subtle incompatibilities withTypedArray. For example, whileArrayBuffer#slice()creates a copy of the slice, the implementation ofBuffer#slice()creates a view over the existingBufferwithout copying, makingBuffer#slice()far more efficient.It is also possible to create new TypedArray instances from a Buffer with the following caveats:
The
Bufferobject's memory is copied to theTypedArray, not shared.The
Bufferobject's memory is interpreted as an array of distinct elements, and not as a byte array of the target type. That is,new Uint32Array(Buffer.from([1, 2, 3, 4]))creates a 4-elementUint32Arraywith elements[1, 2, 3, 4], not aUint32Arraywith a single element[0x1020304]or[0x4030201].
在引入 之前
TypedArray,JavaScript 语言没有读取或操作二进制数据流的机制。该Buffer班介绍了作为Node.js的API来实现与八位字节流中的TCP流,文件系统操作,以及其他环境交互的一部分。与
TypedArray现在可用的,所述Buffer类实现Uint8Array的方式的API,更优化和适合Node.js的…
缓冲区实例也是
Uint8Array实例。但是,与TypedArray. 例如,在ArrayBuffer#slice()创建切片的副本时,实现Buffer#slice()在Buffer不复制的情况下在现有视图上创建视图,从而Buffer#slice()提高效率。也可以从 Buffer 创建新的 TypedArray 实例,但有以下注意事项:
的
Buffer对象的存储器被复制到TypedArray,而不是共享。的
Buffer对象的存储器被解释为不同的元件的阵列,而不是作为目标类型的一个字节数组。也就是说,new Uint32Array(Buffer.from([1, 2, 3, 4]))创建一个Uint32Array带有元素的 4元素[1, 2, 3, 4],而不是Uint32Array带有单个元素[0x1020304]或的元素[0x4030201]。
They go on to mention TypedArray.from, which in node accepts Buffers, so the 'correct' way is:
他们继续提到TypedArray.from,它在节点中接受Buffers,所以“正确”的方法是:
var arrByte = Uint8Array.from(data)
...however, this shouldn't be necessary at all since a Bufferis a Uint8Arrayand new UintArray(someBuffer)does work just fine.
...但是,这根本没有必要,因为 aBuffer是 aUint8Array并且new UintArray(someBuffer)确实可以正常工作。
There's also no context in your question, but Blobdoesn't exist in node, and you shouldn't need it anyway, since Bufferalready gives you raw access to binary data and the other fsmethods let you read and write files.
您的问题中也没有上下文,但Blob在 node 中不存在,无论如何您都不需要它,因为Buffer已经为您提供了对二进制数据的原始访问权限,而其他fs方法可让您读写文件。
回答by lilKing
import * as fs from 'fs';
[...]
event:(data) => {
fs.readFile(data, function(err, data) {
var arrByte= new Uint8Array.from(Buffer.from(data))
var binaryData= new Blob([arrByte])
if (err) throw err;
console.log(binaryData)
}
}

