In this article, we will learn the conversion of a given byte array to a string of characters. A Byte array is an array containing encoded data in the form of unsigned integers. It can’t be used directly. We have to decode it to get the meaning full data out of it. This article will demonstrate how we can convert the byte array to some meaningful information.
Approaches:
Various approaches to convert byte array to string are as follows:
- Using WebAPI TextDecoder.decode() Method
- Using Buffer and toString() Methods
- Using JavaScript string.fromCharCode() Method
Approach 1: Using WebAPI TextDecoder.decode() Method
The decode() method in TextDecoder API is used to take a stream of bytes as input and emits a stream of code points. The TextEncoder decode() method takes an ArrayBuffer containing the encoded data and options object and returns the original string (i.e. decoded string).
Syntax:
decoder.decode( buffer, options );
Parameters:
- buffer: It is an ArrayBuffer, Dataview, or TypedArray instance that contains the encoded data.
- options: It is an optional parameter that holds an object containing some properties.
- stream: It is a boolean value. It is set to true if data are processing in chunks and false if data are not chunked (by default its value is set to false)
Return Value:
It decodes the encoded input in the buffer and returns the decoded string.
Example: In this example, we will create a string using TextDecoder.decode() method from the Uint8Array instance.
Javascript
// Creating new byte array using // Uint8Array instance let byteArray = new Uint8Array([ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116, ]); // Creating textDecoder instance let decoder = new TextDecoder( "utf-8" ); // Using decode method to get string output let str = decoder.decode(byteArray); // Display the output console.log(str); |
JavaScript
Approach 2: Using Buffer and toString() Methods
Buffers are instances of the Buffer class in Node.js. Buffers are designed to handle binary raw data.
Syntax:
let arr = new Buffer([16, 32, 48, 64]);
The JavaScript Array toString() Method returns the string representation of the array elements
Syntax:
arr.toString();
Example: In this example, we have implemented the Buffer and toString() Methods for converting the byte array to a String.
Javascript
// Creating new input array buffer let byteArray = Buffer.from([ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116, ]); // Converting buffer to string let str = byteArray.toString(); // Display output console.log(str); |
JavaScript
Approach 3: Using JavaScript string.fromCharCode() Method
The JavaScript string.fromCharCode() method is used to create a string from the given sequence.
Syntax:
String.fromCharCode(n1, n2, ..., nX);
Example: In this example, we will use String.fromCharCode() method to get the string output from a given byteArray.
Javascript
// Input byte Array for let byteArray = [ 71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115, ]; // Iterating array using array.map method let str = byteArray .map((byte) => { return String.fromCharCode(byte); }) .join( "" ); // Dipslay the output console.log(str); |
neveropen