Back to the catalog

Binary data in Node.js: encodings, byte order, and shared memory

12 questions

How Buffer, Uint8Array, ArrayBuffer, and DataView relate, how encodings and byte order work, which operations share memory, and why turning binary data into a string destroys it. We ran every code sample in Node to check the answers.

Questions

  1. Not answered. Buffer is a subclass of which built-in JavaScript typed array?
  2. Not answered. What is an ArrayBuffer, and how do you read the bytes inside it?
  3. Not answered. What is new Uint32Array(4).byteLength?
  4. Not answered. Which statements about Buffer versus Uint8Array are correct?
  5. Not answered. How many bytes is the string café in UTF-8?
  6. Not answered. What does Buffer.from('SGVsbG8=', 'base64').toString('utf8') return?
  7. Not answered. You read a binary file with let data = '' and stream.on('data', c => data += c). Why is the result corrupted?
  8. Not answered. Decoding a UTF-8 stream with chunk.toString() on each chunk sometimes produces �. Why?
  9. Not answered. What is [...new Uint8Array([256, -1, 300])]?
  10. Not answered. Read as a 32-bit integer, the bytes 00 00 00 01 give 1 one way and 16777216 the other. What causes the difference?
  11. Not answered. Which of these operations return something that shares memory with the source instead of copying it?
  12. Not answered. What is the difference between Buffer.alloc(1024) and Buffer.allocUnsafe(1024)?