问题描述
这是为什么:
var myArrayBuffer = fs.readFileSync(file, null)
返回一个uInt8数组而不是一个arrayBuffer吗?为什么这似乎行得通?
returning an uInt8 array instead of a just a arrayBuffer? why does this seem to work?
var myArrayBuffer = fs.readFileSync(file, null).buffer;
var myAArray = new Uint16Array( myArrayBuffer.slice(266,(sizeofArray*sizeOfArrayElement));
为什么fs.readFile会将我的文件解析为uInt8数组?没有任何意义,该文件具有许多不同的数据类型,这些数据类型的长度都不是1个字节.
Why would the fs.readFile parse my file into a uInt8 array? Makes no sense, the file has a bunch of different datatypes that are not 1 byte long.
推荐答案
因为自v3.0.0版以来, Buffer
类继承自 Uint8Array
类.引用文档:
Because since v3.0.0 Buffer
class inherits from Uint8Array
class. Quoting the doc:
有可能创建一个新的 Buffer
,该缓冲区与通过使用 TypeArray
对象的 .buffer
属性来 TypedArray
实例.
It is possible to create a new Buffer
that shares the same allocated memory as a TypedArray
instance by using the TypeArray
object's .buffer
property.
...这正是您的示例中所做的.
... which is exactly what's done in your example.
这篇关于节点fs.readFileSync返回一个uInt8数组而不是原始缓冲区数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!