JavaScript - Convert Byte Array to String Last Updated : 26 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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. JavaScript const byteA = new Uint8Array([72, 101, 108, 108, 111]); const s = new TextDecoder().decode(byteA); console.log(s); OutputHello A TextDecoder object is created, which can decode byte arrays.The decode method converts the byte array to a string.2. Using Buffer and toString() Method (Node.js)In Node.js, the Buffer class provides a toString method to decode byte arrays into strings. JavaScript const byteA = Buffer.from([72, 101, 108, 108, 111]); const s = byteA.toString("utf-8"); console.log(s); OutputHello Buffer.from() converts the byte array into a Node.js Buffer.The toString() method decodes the buffer into a string.3. Using String.fromCharCode() MethodThis method uses String.fromCharCode to convert each byte in the array into a character. JavaScript const byteA = [72, 101, 108, 108, 111]; const s = String.fromCharCode(...byteA); console.log(s); OutputHello The spread operator (...) expands the byte array into individual arguments.String.fromCharCode converts each byte to its corresponding character.4. Using Base64 Encoding and DecodingThis approach involves encoding the byte array into Base64 and decoding it to a string. JavaScript const byteA = new Uint8Array([72, 101, 108, 108, 111]); const base64 = btoa(String.fromCharCode(...byteA)); const s = atob(base64); console.log(s); OutputHello btoa encodes the byte array into a Base64 string.atob decodes the Base64 string back to the original string.5. Using TextDecoder.decode() with Node.js BufferYou can combine TextDecoder and Buffer for a modern, efficient approach in Node.js. JavaScript const byteA = Buffer.from([72, 101, 108, 108, 111]); const s = new TextDecoder().decode(byteA); console.log(s); OutputHello Buffer.from() converts the array to a Buffer.TextDecoder.decode() decodes the Buffer to a string.6. Using the String Constructor with Array.join() MethodThis method uses Array.join to create a string representation of the byte array. JavaScript const byteA = [72, 101, 108, 108, 111]; const s = byteA.map(byte => String.fromCharCode(byte)).join(""); console.log(s); OutputHello map() converts each byte to its character.join() concatenates all characters into single string.Which Approach Should You Use?ApproachWhen to UseTextDecoder.decode()Best for modern web or Node.js projects; works well with UTF-8 data.Buffer and toString()Ideal for Node.js environments.String.fromCharCode()Simple and effective for small arrays.Base64 Encoding and DecodingUse when working with Base64 encoding/decoding.TextDecoder.decode() with BufferA modern, efficient combination for Node.js projects.String Constructor with Array.join()Customizable but less efficient for large arrays. Comment More infoAdvertise with us Next Article JavaScript - Convert Byte Array to String J jatinsharmatu54 Follow Improve Article Tags : JavaScript Web Technologies javascript-string javascript-array JavaScript-Questions +1 More Similar Reads Convert base64 String to ArrayBuffer In JavaScript 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 JavaScr 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 JavaScript Program to Convert Byte Array to JSON In this article, we are going to learn about the conversion of a Byte Array into JSON. Converting a byte array to JSON means transforming a sequence of bytes into a structured JSON format, often involving decoding bytes to a text string and then parsing it into JSON data.Example:Input : [71, 101, 10 3 min read JavaScript Program to Convert String to Bytes In this article, we are going to learn ho to Convert String to bytes in JavaScript. Converting a string to bytes in JavaScript involves encoding the characters using a specific character encoding (such as UTF-8) to represent the string as a sequence of bytes. There are several methods that can be us 3 min read How to Convert Byte Array to String in PHP? Given a Byte Array, the task is to convert the byte array to a String in PHP. It is used in various scenarios, such as processing binary data, handling file uploads, or working with data transmission protocols. Below are the approaches to convert byte array to string in PHP:Table of ContentWhat is a 3 min read How to Convert Byte Array to JSON in PHP ? Given a Byte Array, the task is to convert Byte Array into JSON using PHP. Converting a byte array to JSON in PHP is a common task, especially when dealing with binary data or when you want to represent raw data in a JSON format. Table of Content Using base64_encode() and json_encode() FunctionsUsin 2 min read How to Convert File Content to Byte Array in PHP ? Converting file content to a byte array in PHP is a useful technique for various applications, including file manipulation, data processing, and when working with binary files like images or PDFs. PHP offers multiple ways to read file content and convert it into a byte array, providing flexibility t 5 min read Java Program to Convert String to Byte Array Using getBytes() Method In Java, strings are objects that are backed internally by a char array. So to convert a string to a byte array, we need a getByte() method. It is the easiest way to convert a string to a byte array. This method converts the given string to a sequence of bytes using the platform's default charset an 2 min read Convert String to Byte Array in Java Using getBytes(Charset) Method To convert a string to a byte array, we use the getBytes(Charset) method In Java that transforms the string into a sequence of bytes based on a specified character encoding. This method returns the byte array and relies on the Charset class to handle character-to-byte mappings.Example:Java// Java pr 3 min read Convert a String to a ByteBuffer in Java In Java, ByteBuffer can be used to perform operations at the Byte level one more thing is this class provides different types of methods for reading writing, and manipulating bytes in a structured way only. In this article, we will learn about String to ByteBuffer in Java. Java Program to Convert St 4 min read Like