Concept Flow - Reading and writing buffer data
Create Buffer
Write Data to Buffer
Read Data from Buffer
Use or Display Data
End
This flow shows how to create a buffer, write data into it, then read data back for use.
Jump into concepts and practice - no test required
const buf = Buffer.alloc(5); buf.write('Hello'); const readStr = buf.toString('utf8'); console.log(readStr);
| Step | Action | Buffer Content (hex) | Buffer Content (string) | Output |
|---|---|---|---|---|
| 1 | Create buffer of length 5 | 00 00 00 00 00 | ||
| 2 | Write 'Hello' to buffer | 48 65 6c 6c 6f | Hello | |
| 3 | Read buffer as UTF-8 string | 48 65 6c 6c 6f | Hello | |
| 4 | Print read string | 48 65 6c 6c 6f | Hello | Hello |
| 5 | End of execution | 48 65 6c 6c 6f | Hello | Execution complete |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| buf | undefined | <Buffer 00 00 00 00 00> | <Buffer 48 65 6c 6c 6f> | <Buffer 48 65 6c 6c 6f> | <Buffer 48 65 6c 6c 6f> |
| readStr | undefined | undefined | undefined | Hello | Hello |
Buffer basics in Node.js:
- Create with Buffer.alloc(size)
- Write string with buf.write('text')
- Read string with buf.toString('utf8')
- Buffer size limits data stored
- Useful for handling raw binary dataBuffer in Node.js?Buffer.alloc(size) creates a zero-filled buffer.Buffer.new and Buffer.create do not exist; new Buffer() is deprecated and unsafe.const buf = Buffer.from('abc');
console.log(buf[1]);Buffer.from('abc') creates a buffer with ASCII codes of 'a', 'b', 'c'. Index 1 is 'b'.const buf = Buffer.alloc(3);
buf.write('hello');
console.log(buf.toString());src to another buffer dest starting at index 2 in dest. Which code correctly does this?source.copy(target, targetStart, sourceStart, sourceEnd).src starting at 0 to 4 bytes, into dest starting at index 2.