Skip to content

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

Why does this happen

> Buffer.from('x79', 'utf8')
<Buffer 79>
> Buffer.from('x80', 'utf8')
<Buffer c2 80>

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


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:

> Buffer.from('x79', 'ascii')
<Buffer 79>
> Buffer.from('x80', 'ascii')
<Buffer 80>

> Buffer.from('💻', 'ascii')
<Buffer 3d bb>
> Buffer.from('💻', 'utf8')
<Buffer f0 9f 92 bb>