string.FromCharCode()
can be useful to convert a byte array into a readable string.
console.log(String.fromCharCode(65, 66, 67));
// ABC
Here's what this would look like when encoding a string with TextEncoder
, and then converting this string back to readable text using fromCharCode
:
const encoded = new TextEncoder().encode('food') // creates a Uint8Array containing [102, 111, 111, 100]
console.log(
// we first need to convert the Uint8Array to a regular array using Array.from()
// then, we destructure this array into fromCharCode()
String.fromCharCode(...Array.from(encoded))
)
// food