const buf = Buffer.from([72, 101, 108, 108, 111]); console.log(buf.toString());
The buffer contains ASCII codes for the letters H, e, l, l, o. Calling toString() converts these codes to their character equivalents, resulting in the string "Hello".
const buf = Buffer.from('68656c6c6f', 'hex'); console.log(buf.toString('utf8'));
The buffer is created from the hex string representing the ASCII codes for "hello". Converting it to a UTF-8 string prints "hello".
const buf = Buffer.from([0xff, 0xfe, 0xfd]); console.log(buf.toString('utf8'));
Invalid UTF-8 bytes are replaced by the Unicode replacement character � (U+FFFD) when converted to string, so the output is three replacement characters.
The toString method accepts an encoding parameter like 'base64' to convert the buffer to a base64 string. Other methods do not exist or are incorrect.
const buf = Buffer.from('c3a9', 'utf8'); console.log(buf.toString());
The string 'c3a9' is a hex representation but is passed as a UTF-8 string, so the buffer contains bytes for characters 'c', '3', 'a', '9' instead of the intended hex bytes. This causes garbled output.