How to useJavaScript string.fromCharCode() Method in Javascript
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.
// 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);
Output
w3wiki
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