Node.js Buffers & Binary Data
How Buffer, Uint8Array, ArrayBuffer, and DataView fit together; UTF-8 / hex / base64 encodings; byte order; which operations share memory versus copy; and why turning binary into a string destroys it. Every answer is verified by running the code in Node.
Questions
- Not answered.
Bufferis a subclass of which built-in JavaScript typed array? - Not answered. What is an
ArrayBuffer, and how do you read the bytes inside it? - Not answered. What is
new Uint32Array(4).byteLength? - Not answered. Which statements about
BufferversusUint8Arrayare correct? - Not answered. How many bytes is the string
caféin UTF-8? - Not answered. What does
Buffer.from('SGVsbG8=', 'base64').toString('utf8')return? - Not answered. Reading a binary file, you write
let data = ''thenstream.on('data', c => data += c). Why is the result corrupted? - Not answered. Decoding a UTF-8 stream as
chunk.toString()per chunk sometimes yields�. Why? - Not answered. What is
[...new Uint8Array([256, -1, 300])]? - Not answered. The bytes
00 00 00 01read as a 32-bit integer give1one way and16777216the other. What causes the difference? - Not answered. Which of these operations return something that shares memory with the source rather than copying it?
- Not answered. What is the difference between
Buffer.alloc(1024)andBuffer.allocUnsafe(1024)?