Categories
JavaScript Answers

How to convert base64 string to ArrayBuffer with JavaScript?

Spread the love

Sometimes, we want to convert base64 string to ArrayBuffer with JavaScript.

In this article, we’ll look at how to convert base64 string to ArrayBuffer with JavaScript.

How to convert base64 string to ArrayBuffer with JavaScript?

To convert base64 string to ArrayBuffer with JavaScript, we can use the Uint8Array.from method.

For instance, we write

const b = Uint8Array.from(atob(base64String), (c) => c.charCodeAt(0));

to call Uint8Array.from with atob(base64String) and (c) => c.charCodeAt(0) to convert the base64String to an ArrayBuffer.

atob decodes the base64 string into an ASCII string.

charCodeAt returns the integer Unicode character code for the first character in each ASCII character string.

Conclusion

To convert base64 string to ArrayBuffer with JavaScript, we can use the Uint8Array.from method.

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 *