Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
Read/Write a typed array to file with node.js

I have the following typed array: Uint16Array(2) [ 60891, 11722 ] which I would like to save to a (binary) file. I would then like to read it back into another Uint16Array with the order of the elements preserved.

I tried using the fs module but when I go to read the file I'm not getting the same integers:

//console.log(buffer) : Uint16Array(2) [ 60891, 11722 ]
fs.writeFileSync(bufferPath, buffer, 'binary')
const loadedBuffer = fs.readFileSync(bufferPath)
//console.log(new Uint16Array(loadedBuffer)) : Uint16Array(4) [ 219, 237, 202, 45 ]

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This happens because the Buffer class is a subclass of JavaScript's Uint8Array class and not a Uint16Array array as you commented in your code. To obtain a copy of the original Uint16Array array you can use the new Uint16Array(buffer, byteOffset, length) constructor like mentioned in the Uint16Array documentation:

let arr = new Uint16Array(loadedBuffer.buffer, loadedBuffer.byteOffset, loadedBuffer.length / 2);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!