从缓冲区数据到字节数组的nodejs转换 [英] nodejs conversion from buffer data to byte array
问题描述
我想将缓冲区数据转换为字节数组.这是我尝试过的
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.
推荐答案
The Buffer
docs are very enlightening:
在引入
TypedArray
之前,JavaScript语言没有用于读取或处理二进制数据流的机制.Buffer
类是作为Node.js API的一部分引入的,以允许与TCP流,文件系统操作和其他上下文中的八位位组流进行交互.
Prior to the introduction of
TypedArray
, the JavaScript language had no mechanism for reading or manipulating streams of binary data. TheBuffer
class was introduced as part of the Node.js API to enable interaction with octet streams in TCP streams, file system operations, and other contexts.
TypedArray
现在可用, Buffer
类以更优化且更适合Node.js的方式实现Uint8Array
API.
With TypedArray
now available, the Buffer
class implements the Uint8Array
API in a manner that is more optimized and suitable for Node.js.
...
缓冲区实例也是Uint8Array
实例..但是,与TypedArray
存在细微的不兼容.例如,当ArrayBuffer#slice()
创建切片的副本时,Buffer#slice()
的实现在现有的Buffer
上创建视图而不进行复制,从而使Buffer#slice()
的效率大大提高.
Buffer instances are also Uint8Array
instances. However, there are subtle incompatibilities with TypedArray
. For example, while ArrayBuffer#slice()
creates a copy of the slice, the implementation of Buffer#slice()
creates a view over the existing Buffer
without copying, making Buffer#slice()
far more efficient.
还可以通过以下注意事项从Buffer创建新的TypedArray实例:
It is also possible to create new TypedArray instances from a Buffer with the following caveats:
-
Buffer
对象的内存被复制到TypedArray
,而不是共享的.
The
Buffer
object's memory is copied to theTypedArray
, not shared.
Buffer
对象的内存被解释为不同元素的数组,而不是目标类型的字节数组.也就是说,new Uint32Array(Buffer.from([1, 2, 3, 4]))
创建具有元素[1, 2, 3, 4]
的4元素Uint32Array
,而不是具有单个元素[0x1020304]
或[0x4030201]
的Uint32Array
.
The Buffer
object'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-element Uint32Array
with elements [1, 2, 3, 4]
, not a Uint32Array
with a single element [0x1020304]
or [0x4030201]
.
他们继续提到 TypedArray.from
,它在节点中接受Buffer
,因此正确"方式是:
They go on to mention TypedArray.from
, which in node accepts Buffer
s, so the 'correct' way is:
var arrByte = Uint8Array.from(data)
...但是,这完全没有必要,因为Buffer
是Uint8Array
,而new UintArray(someBuffer)
确实可以正常工作.
...however, this shouldn't be necessary at all since a Buffer
is a Uint8Array
and new UintArray(someBuffer)
does work just fine.
您的问题中也没有上下文,但是Blob
在节点中不存在,而且您也不应该使用它,因为Buffer
已经为您提供了对二进制数据和其他fs
方法的原始访问权限让您读取和写入文件.
There's also no context in your question, but Blob
doesn't exist in node, and you shouldn't need it anyway, since Buffer
already gives you raw access to binary data and the other fs
methods let you read and write files.
这篇关于从缓冲区数据到字节数组的nodejs转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!