Skip to content
Advertisement

Why does Buffer.from(‘x80’, ‘utf8’) return

Why does this happen

JavaScript

and how do I get Buffer to behave how I expect and return a <Buffer 80> instead?

Advertisement

Answer

This happens because 0x80 or 1000 0000 in binary or 128 in decimal is not a valid code point in UTF-8 because it is outside of ASCII (which is 7 bits, so all ASCII code points have the first bit set to 0). To convert strings to Buffers without interpreting them as UTF-8, you can use the 'ascii' encoding instead:

JavaScript
Advertisement