Categories
JavaScript Answers

How to convert a string to byte array with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *