Convert base64 String to ArrayBuffer In JavaScript Last Updated : 13 Sep, 2024 Comments Improve Suggest changes Like Article Like Report A Base64 string represents binary data in an ASCII string format by translating it into a radix-64 representation. Often used to encode binary data in text-based formats like JSON or HTML, it needs to be converted back into its original binary format for further processing. An ArrayBuffer in JavaScript is a generic, fixed-length binary data buffer, which is useful when working with raw binary data such as images or files. Converting a Base64 string to an ArrayBuffer is a common task in web development, especially when dealing with binary data that has been encoded for transmission over networks.Below are the approaches to convert base64 string to ArrayBuffer in Javascript:Table of ContentUsing atob and Uint8ArrayUsing TextDecoder and Uint8Array1. Using atob and Uint8ArrayThe atob() function decodes a Base64-encoded string into a binary string. The Uint8Array constructor can then be used to convert this binary string into an ArrayBuffer.Example: This function converts a Base64-encoded string into an ArrayBuffer. JavaScript function base64ToArrayBuffer(base64) { const binaryString = atob(base64); const length = binaryString.length; const bytes = new Uint8Array(length); for (let i = 0; i < length; i++) { bytes[i] = binaryString.charCodeAt(i); } return bytes.buffer; } const base64String = "SGVsbG8gd29ybGQ="; const arrayBuffer = base64ToArrayBuffer(base64String); console.log(arrayBuffer); OutputArrayBuffer { [Uint8Contents]: <48 65 6c 6c 6f 20 77 6f 72 6c 64>, byteLength: 11}2. Using TextDecoder and Uint8ArrayThis approach is similar to the first but uses the TextDecoder interface to convert the binary string to an ArrayBuffer. This method is more modern and offers better performance with the UTF-8 format.Example: This function converts a Base64-encoded string into an ArrayBuffer using the TextEncoder to encode the binary string. JavaScript function base64ToArrayBuffer(base64) { const binaryString = atob(base64); const encoder = new TextEncoder(); const binaryArray = encoder.encode(binaryString); return binaryArray.buffer; } const base64String = "SGVsbG8gd29ybGQ="; const arrayBuffer = base64ToArrayBuffer(base64String); console.log(arrayBuffer); OutputArrayBuffer { [Uint8Contents]: <48 65 6c 6c 6f 20 77 6f 72 6c 64>, byteLength: 11} Comment More infoAdvertise with us Next Article Convert base64 String to ArrayBuffer In JavaScript A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads Convert Array to String in JavaScript In JavaScript, converting an array to a string involves combining its elements into a single text output, often separated by a specified delimiter. This is useful for displaying array contents in a readable format or when storing data as a single string. The process can be customized to use differen 7 min read JavaScript - Convert Byte Array to String Here are the various methods to convert Byte Array to string in JavaScript.1. Using WebAPI TextDecoder.decode() MethodThe TextDecoder API is a modern and efficient way to convert a byte array (Uint8Array) to a string. Itâs supported in both browsers and Node.js.JavaScriptconst byteA = new Uint8Array 2 min read How to Convert JSON to ArrayBuffer in JavaScript? An ArrayBuffer is a complex data type, and structures which has a fixed length and takes binary content as the whole. Any variable that contains pure binary data will be defined in JavaScript as Simple Data, however on some occasions it is sometimes necessary to convert JSON data to an ArrayBuffer, 4 min read How To Convert Base64 to JSON String in JavaScript? There could be situations in web applications, where there is a need to decode the data from Base64 format back into its original JSON format. It generally happens when one has to transmit data over the network where Base64 encoding is well suited for encoding binary data.In this article, we will se 2 min read JavaScript | Encode/Decode a string to Base64 To encode or decode strings in JavaScript, we can use the built-in functions provided by the language. These functions help in encoding special characters in a URL or decoding encoded strings back to their original form. 1. btoa() MethodThis method encodes a string in base-64 and uses the "A-Z", "a- 6 min read Convert an Array to JSON in JavaScript Given a JavaScript Array and the task is to convert an array to JSON Object. Below are the approaches to convert an array to JSON using JsvaScript: Table of ContentJSON.stringify() methodObject.assign() methodJSON.stringify() methodThe use of JSON is to exchange data to/from a web server. While send 2 min read How to Convert JSON to base64 in JavaScript ? Base 64 is the encoding scheme that represents binary data in a printable ASCII format, commonly used for data serialization and transmission. Table of Content Using btoa functionUsing Manual ConversionUsing btoa functionIn this approach, we're using btoa to encode a UTF-8 string representation of a 2 min read How to convert image into base64 string using JavaScript ? In this article, we will convert an image into a base64 string using Javascript. The below approaches show the methods to convert an image into a base64 string using Javascript.ApproachHere we will create a gfg.js file which will include JavaScript code and one gfg.html file.Now we will put onchange 2 min read How to Convert Base64 to File in JavaScript? In web development, Base64 encoding is often used to represent binary data, such as images or files, with a string of ASCII characters, sometimes you may be required to change this Base64 string back into a file for instance for file uploads, downloads, or processing in the browser, this article aim 2 min read How to Convert Base64 to Blob in JavaScript? Working with files and data in web applications often involves dealing with binary data. One common scenario is converting a Base64 string into a Blob object, which can then be used in various ways, such as creating downloadable files or uploading images to a server. This article will guide you thro 4 min read Like