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:
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.With
TypedArray
now available, theBuffer
class implements theUint8Array
API in a manner that is more optimized and suitable for Node.js.…
Buffer instances are also
Uint8Array
instances. 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 existingBuffer
without copying, makingBuffer#slice()
far more efficient.It is also possible to create new TypedArray instances from a Buffer with the following caveats:
The
Buffer
object's memory is copied to theTypedArray
, not shared.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-elementUint32Array
with elements[1, 2, 3, 4]
, not aUint32Array
with a single element[0x1020304]
or[0x4030201]
.
They go on to mention TypedArray.from
, which in node accepts Buffer
s, so the 'correct' way is:
var arrByte = Uint8Array.from(data)
...however, this shouldn't be necessary at all since a Buffer
is a Uint8Array
and new UintArray(someBuffer)
does work just fine.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With