How to useBuffer and toString() Methods in Javascript
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.
// 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);
Output
JavaScript
How to Convert Byte Array to String in JavaScript ?
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. In this article, we will learn the conversion of a given byte array to a string of characters.
Various approaches to convert byte array to string are as follows:
Table of Content
- Using WebAPI TextDecoder.decode() Method
- Using Buffer and toString() Methods
- Using JavaScript string.fromCharCode() Method
- Using Base64 Encoding and Decoding
- Using TextDecoder.decode() with Node.js Buffer
- Using the String constructor with Array.join