18

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.

c69
  • 19,951
  • 7
  • 52
  • 82
lilKing
  • 365
  • 1
  • 3
  • 8

2 Answers2

20

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. 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.

With TypedArray now available, the Buffer class implements the Uint8Array API in a manner that is more optimized and suitable for Node.js.

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.

It is also possible to create new TypedArray instances from a Buffer with the following caveats:

  1. The Buffer object's memory is copied to the TypedArray, not shared.

  2. 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].

They go on to mention TypedArray.from, which in node accepts Buffers, 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.

josh3736
  • 139,160
  • 33
  • 216
  • 263
  • 1
    Thanks for the post. However, logging arrByte gives this output `Uint8Array [ ]` – lilKing Jul 25 '18 at 06:13
  • And what is your buffer (`data`)? What is `err`? I bet you're getting an error and ignoring it. Never skip error checking -- ***always*** check `err` in node callbacks. – josh3736 Jul 25 '18 at 06:24
  • this is the buffer data from fingerprint renderer process `` which I want to convert to byte[] and I've also checked `if (err) throw err`... Guess there's no err – lilKing Jul 25 '18 at 08:48
  • i was able to get `Uint8Array(55797) [255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 1, 1, 244, ...]` when i set arrByte to `Uint8Array.from(Buffer.from(data))` Thanks a lot – lilKing Jul 25 '18 at 17:14
  • @lilKing Hi, can you pls share the code here, because iam also having the same issue converting buffer data to byte array. – MidhunKrishna Mar 21 '19 at 06:05
  • @MidhunKrishna I'll post it shortly. Hope it helps – lilKing Mar 22 '19 at 11:37
  • There is an easier way to do this outlined in this answer: https://stackoverflow.com/a/55127012/11544830 – toinhao Jul 29 '22 at 12:00
8
  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)
    }
 }
lilKing
  • 365
  • 1
  • 3
  • 8
  • What is the type of "data" variable? – Vikas Satpute Jan 20 '20 at 07:24
  • 1
    @VikasSatpute in the outer context (i.e. as the first parameter to fs.readFile) it's the file name; in the inner context it's the data read from the file. In my tests, the inner data is already of type Buffer so you don't need to call Buffer.from(data) at all. – Andy Apr 03 '20 at 14:41