Sometimes, we want to convert a string to byte array with JavaScript.
In this article, we’ll look at how to convert a string to byte array with JavaScript.
How to convert a string to byte array with JavaScript?
To convert a string to byte array with JavaScript, we can use the TextEncoder
constructor.
For instance, we write
const utf8Encode = new TextEncoder();
const byteArr = utf8Encode.encode("abc");
to create a TextEncoder
object.
Then we call utf8Encode.encode
with a string to return a byte array converted from the string.
Conclusion
To convert a string to byte array with JavaScript, we can use the TextEncoder
constructor.