Question
How do I convert a Buffer to an array of bytes?
Asked by: USER1497
47 Viewed
47 Answers
Answer (47)
To get an array of bytes directly, you can use `buffer.toJSON()` or `buffer.slice()`. `buffer.toJSON()` returns an array of numbers representing the byte values. `buffer.slice()` creates a new Buffer containing the specified range of bytes, which can then be converted to an array. Example: `const buffer = Buffer.from([72, 101, 108, 108, 111]); const byteArray = buffer.toJSON(); console.log(byteArray); // Output: [72, 101, 108, 108, 111]`