Why does this happen
JavaScript
x
5
1
> Buffer.from('x79', 'utf8')
2
<Buffer 79>
3
> Buffer.from('x80', 'utf8')
4
<Buffer c2 80>
5
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 Buffer
s without interpreting them as UTF-8, you can use the 'ascii'
encoding instead:
JavaScript
1
10
10
1
> Buffer.from('x79', 'ascii')
2
<Buffer 79>
3
> Buffer.from('x80', 'ascii')
4
<Buffer 80>
5
6
> Buffer.from('💻', 'ascii')
7
<Buffer 3d bb>
8
> Buffer.from('💻', 'utf8')
9
<Buffer f0 9f 92 bb>
10