Complete the code to allocate a buffer of 10 bytes.
const buf = Buffer.[1](10);
The Buffer.alloc(size) method creates a buffer of the specified size filled with zeros.
Complete the code to create a buffer from the string 'hello' using UTF-8 encoding.
const buf = Buffer.from('hello', '[1]');
The default and most common encoding for strings is utf8. It encodes characters as UTF-8 bytes.
Fix the error in the code to convert a buffer to a string with base64 encoding.
const str = buf.toString('[1]');
To convert a buffer to a base64 encoded string, use buf.toString('base64').
Fill both blanks to create a buffer from a hex string and convert it back to a utf8 string.
const buf = Buffer.[1]('68656c6c6f', '[2]'); const str = buf.toString('utf8');
Use Buffer.from to create a buffer from a string, specifying the input encoding as hex.
Fill all three blanks to allocate a buffer, write a string with utf8 encoding, and convert it to base64.
const buf = Buffer.[1](6); buf.write('hello', 0, [2], '[3]'); const base64 = buf.toString('base64');
Allocate a buffer of size 6 with Buffer.alloc. Write the string 'hello' starting at offset 0, with length 5, using 'utf8' encoding. Then convert to base64 string.