Uint8Array.toString()
but not the other way around. Does anyone know of an easy way for me to do this without creating my own parser?You can use Buffer.from(string[, encoding])
. The Buffer
class has implemented the Uint8Array
interface in Node since v4.x. You can also optionally specify an encoding with which to do string processing in both directions, i.e. buffer.toString([encoding])
.
Uint8Array.from(text.split('').map(letter => letter.charCodeAt(0)));
or (appears about 18% faster):
Uint8Array.from(Array.from(text).map(letter => letter.charCodeAt(0)));
Note: These only work for ASCII, so all the letters are modulo 256, so if you try with Russian text, it won't work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With