从缓冲区数据到字节数组的nodejs转换 [英] nodejs conversion from buffer data to byte array

查看:839
本文介绍了从缓冲区数据到字节数组的nodejs转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将缓冲区数据转换为字节数组.这是我尝试过的

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.

推荐答案

Buffer文档是很有启发性:

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. The Buffer 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:

  1. Buffer对象的内存被复制到TypedArray,而不是共享的.

  1. The Buffer object's memory is copied to the TypedArray, 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 Buffers, so the 'correct' way is:

var arrByte = Uint8Array.from(data)

...但是,这完全没有必要,因为BufferUint8Array,而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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆